Profile-Modify.php 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492
  1. <?php
  2. /**
  3. * This file has the primary job of showing and editing people's profiles.
  4. * It also allows the user to change some of their or another's preferences,
  5. * and such things
  6. *
  7. * Simple Machines Forum (SMF)
  8. *
  9. * @package SMF
  10. * @author Simple Machines http://www.simplemachines.org
  11. * @copyright 2011 Simple Machines
  12. * @license http://www.simplemachines.org/about/smf/license.php BSD
  13. *
  14. * @version 2.1 Alpha 1
  15. */
  16. if (!defined('SMF'))
  17. die('Hacking attempt...');
  18. /**
  19. * This defines every profile field known to man.
  20. *
  21. * @param bool $force_reload = false
  22. */
  23. function loadProfileFields($force_reload = false)
  24. {
  25. global $context, $profile_fields, $txt, $scripturl, $modSettings, $user_info, $old_profile, $smcFunc, $cur_profile, $language;
  26. // Don't load this twice!
  27. if (!empty($profile_fields) && !$force_reload)
  28. return;
  29. /* This horrific array defines all the profile fields in the whole world!
  30. In general each "field" has one array - the key of which is the database column name associated with said field. Each item
  31. can have the following attributes:
  32. string $type: The type of field this is - valid types are:
  33. - callback: This is a field which has its own callback mechanism for templating.
  34. - check: A simple checkbox.
  35. - hidden: This doesn't have any visual aspects but may have some validity.
  36. - password: A password box.
  37. - select: A select box.
  38. - text: A string of some description.
  39. string $label: The label for this item - default will be $txt[$key] if this isn't set.
  40. string $subtext: The subtext (Small label) for this item.
  41. int $size: Optional size for a text area.
  42. array $input_attr: An array of text strings to be added to the input box for this item.
  43. string $value: The value of the item. If not set $cur_profile[$key] is assumed.
  44. string $permission: Permission required for this item (Excluded _any/_own subfix which is applied automatically).
  45. function $input_validate: A runtime function which validates the element before going to the database. It is passed
  46. the relevant $_POST element if it exists and should be treated like a reference.
  47. Return types:
  48. - true: Element can be stored.
  49. - false: Skip this element.
  50. - a text string: An error occured - this is the error message.
  51. function $preload: A function that is used to load data required for this element to be displayed. Must return
  52. true to be displayed at all.
  53. string $cast_type: If set casts the element to a certain type. Valid types (bool, int, float).
  54. string $save_key: If the index of this element isn't the database column name it can be overriden
  55. with this string.
  56. bool $is_dummy: If set then nothing is acted upon for this element.
  57. bool $enabled: A test to determine whether this is even available - if not is unset.
  58. string $link_with: Key which links this field to an overall set.
  59. Note that all elements that have a custom input_validate must ensure they set the value of $cur_profile correct to enable
  60. the changes to be displayed correctly on submit of the form.
  61. */
  62. $profile_fields = array(
  63. 'aim' => array(
  64. 'type' => 'text',
  65. 'label' => $txt['aim'],
  66. 'subtext' => $txt['your_aim'],
  67. 'size' => 24,
  68. 'value' => strtr(empty($cur_profile['aim']) ? '' : $cur_profile['aim'], '+', ' '),
  69. 'permission' => 'profile_extra',
  70. 'input_validate' => create_function('&$value', '
  71. $value = strtr($value, \' \', \'+\');
  72. return true;
  73. '),
  74. ),
  75. 'avatar_choice' => array(
  76. 'type' => 'callback',
  77. 'callback_func' => 'avatar_select',
  78. // This handles the permissions too.
  79. 'preload' => 'profileLoadAvatarData',
  80. 'input_validate' => 'profileSaveAvatarData',
  81. 'save_key' => 'avatar',
  82. ),
  83. 'bday1' => array(
  84. 'type' => 'callback',
  85. 'callback_func' => 'birthdate',
  86. 'permission' => 'profile_extra',
  87. 'preload' => create_function('', '
  88. global $cur_profile, $context;
  89. // Split up the birthdate....
  90. list ($uyear, $umonth, $uday) = explode(\'-\', empty($cur_profile[\'birthdate\']) || $cur_profile[\'birthdate\'] == \'0001-01-01\' ? \'0000-00-00\' : $cur_profile[\'birthdate\']);
  91. $context[\'member\'][\'birth_date\'] = array(
  92. \'year\' => $uyear == \'0004\' ? \'0000\' : $uyear,
  93. \'month\' => $umonth,
  94. \'day\' => $uday,
  95. );
  96. return true;
  97. '),
  98. 'input_validate' => create_function('&$value', '
  99. global $profile_vars, $cur_profile;
  100. if (isset($_POST[\'bday2\'], $_POST[\'bday3\']) && $value > 0 && $_POST[\'bday2\'] > 0)
  101. {
  102. // Set to blank?
  103. if ((int) $_POST[\'bday3\'] == 1 && (int) $_POST[\'bday2\'] == 1 && (int) $value == 1)
  104. $value = \'0001-01-01\';
  105. else
  106. $value = checkdate($value, $_POST[\'bday2\'], $_POST[\'bday3\'] < 4 ? 4 : $_POST[\'bday3\']) ? sprintf(\'%04d-%02d-%02d\', $_POST[\'bday3\'] < 4 ? 4 : $_POST[\'bday3\'], $_POST[\'bday1\'], $_POST[\'bday2\']) : \'0001-01-01\';
  107. }
  108. else
  109. $value = \'0001-01-01\';
  110. $profile_vars[\'birthdate\'] = $value;
  111. $cur_profile[\'birthdate\'] = $value;
  112. return false;
  113. '),
  114. ),
  115. // Setting the birthdate the old style way?
  116. 'birthdate' => array(
  117. 'type' => 'hidden',
  118. 'permission' => 'profile_extra',
  119. 'input_validate' => create_function('&$value', '
  120. global $cur_profile;
  121. // @todo Should we check for this year and tell them they made a mistake :P? (based on coppa at least?)
  122. if (preg_match(\'/(\d{4})[\-\., ](\d{2})[\-\., ](\d{2})/\', $value, $dates) === 1)
  123. {
  124. $value = checkdate($dates[2], $dates[3], $dates[1] < 4 ? 4 : $dates[1]) ? sprintf(\'%04d-%02d-%02d\', $dates[1] < 4 ? 4 : $dates[1], $dates[2], $dates[3]) : \'0001-01-01\';
  125. return true;
  126. }
  127. else
  128. {
  129. $value = empty($cur_profile[\'birthdate\']) ? \'0001-01-01\' : $cur_profile[\'birthdate\'];
  130. return false;
  131. }
  132. '),
  133. ),
  134. 'date_registered' => array(
  135. 'type' => 'text',
  136. 'value' => empty($cur_profile['date_registered']) ? $txt['not_applicable'] : strftime('%Y-%m-%d', $cur_profile['date_registered'] + ($user_info['time_offset'] + $modSettings['time_offset']) * 3600),
  137. 'label' => $txt['date_registered'],
  138. 'log_change' => true,
  139. 'permission' => 'moderate_forum',
  140. 'input_validate' => create_function('&$value', '
  141. global $txt, $user_info, $modSettings, $cur_profile, $context;
  142. // Bad date! Go try again - please?
  143. if (($value = strtotime($value)) === -1)
  144. {
  145. $value = $cur_profile[\'date_registered\'];
  146. return $txt[\'invalid_registration\'] . \' \' . strftime(\'%d %b %Y \' . (strpos($user_info[\'time_format\'], \'%H\') !== false ? \'%I:%M:%S %p\' : \'%H:%M:%S\'), forum_time(false));
  147. }
  148. // As long as it doesn\'t equal "N/A"...
  149. elseif ($value != $txt[\'not_applicable\'] && $value != strtotime(strftime(\'%Y-%m-%d\', $cur_profile[\'date_registered\'] + ($user_info[\'time_offset\'] + $modSettings[\'time_offset\']) * 3600)))
  150. $value = $value - ($user_info[\'time_offset\'] + $modSettings[\'time_offset\']) * 3600;
  151. else
  152. $value = $cur_profile[\'date_registered\'];
  153. return true;
  154. '),
  155. ),
  156. 'email_address' => array(
  157. 'type' => 'text',
  158. 'label' => $txt['user_email_address'],
  159. 'subtext' => $txt['valid_email'],
  160. 'log_change' => true,
  161. 'permission' => 'profile_identity',
  162. 'input_validate' => create_function('&$value', '
  163. global $context, $old_profile, $context, $profile_vars, $sourcedir, $modSettings;
  164. if (strtolower($value) == strtolower($old_profile[\'email_address\']))
  165. return false;
  166. $isValid = profileValidateEmail($value, $context[\'id_member\']);
  167. // Do they need to revalidate? If so schedule the function!
  168. if ($isValid === true && !empty($modSettings[\'send_validation_onChange\']) && !allowedTo(\'moderate_forum\'))
  169. {
  170. require_once($sourcedir . \'/Subs-Members.php\');
  171. $profile_vars[\'validation_code\'] = generateValidationCode();
  172. $profile_vars[\'is_activated\'] = 2;
  173. $context[\'profile_execute_on_save\'][] = \'profileSendActivation\';
  174. unset($context[\'profile_execute_on_save\'][\'reload_user\']);
  175. }
  176. return $isValid;
  177. '),
  178. ),
  179. 'gender' => array(
  180. 'type' => 'select',
  181. 'cast_type' => 'int',
  182. 'options' => 'return array(0 => \'\', 1 => $txt[\'male\'], 2 => $txt[\'female\']);',
  183. 'label' => $txt['gender'],
  184. 'permission' => 'profile_extra',
  185. ),
  186. 'hide_email' => array(
  187. 'type' => 'check',
  188. 'value' => empty($cur_profile['hide_email']) ? true : false,
  189. 'label' => $txt['allow_user_email'],
  190. 'permission' => 'profile_identity',
  191. 'input_validate' => create_function('&$value', '
  192. $value = $value == 0 ? 1 : 0;
  193. return true;
  194. '),
  195. ),
  196. 'icq' => array(
  197. 'type' => 'text',
  198. 'label' => $txt['icq'],
  199. 'subtext' => $txt['your_icq'],
  200. 'size' => 24,
  201. 'permission' => 'profile_extra',
  202. // Need to make sure ICQ doesn't equal 0.
  203. 'input_validate' => create_function('&$value', '
  204. if (empty($value))
  205. $value = \'\';
  206. else
  207. $value = (int) $value;
  208. return true;
  209. '),
  210. ),
  211. // Selecting group membership is a complicated one so we treat it separate!
  212. 'id_group' => array(
  213. 'type' => 'callback',
  214. 'callback_func' => 'group_manage',
  215. 'permission' => 'manage_membergroups',
  216. 'preload' => 'profileLoadGroups',
  217. 'log_change' => true,
  218. 'input_validate' => 'profileSaveGroups',
  219. ),
  220. 'id_theme' => array(
  221. 'type' => 'callback',
  222. 'callback_func' => 'theme_pick',
  223. 'permission' => 'profile_extra',
  224. 'enabled' => $modSettings['theme_allow'] || allowedTo('admin_forum'),
  225. 'preload' => create_function('', '
  226. global $smcFunc, $context, $cur_profile, $txt;
  227. $request = $smcFunc[\'db_query\'](\'\', \'
  228. SELECT value
  229. FROM {db_prefix}themes
  230. WHERE id_theme = {int:id_theme}
  231. AND variable = {string:variable}
  232. LIMIT 1\', array(
  233. \'id_theme\' => $cur_profile[\'id_theme\'],
  234. \'variable\' => \'name\',
  235. )
  236. );
  237. list ($name) = $smcFunc[\'db_fetch_row\']($request);
  238. $smcFunc[\'db_free_result\']($request);
  239. $context[\'member\'][\'theme\'] = array(
  240. \'id\' => $cur_profile[\'id_theme\'],
  241. \'name\' => empty($cur_profile[\'id_theme\']) ? $txt[\'theme_forum_default\'] : $name
  242. );
  243. return true;
  244. '),
  245. 'input_validate' => create_function('&$value', '
  246. $value = (int) $value;
  247. return true;
  248. '),
  249. ),
  250. 'karma_good' => array(
  251. 'type' => 'callback',
  252. 'callback_func' => 'karma_modify',
  253. 'permission' => 'admin_forum',
  254. // Set karma_bad too!
  255. 'input_validate' => create_function('&$value', '
  256. global $profile_vars, $cur_profile;
  257. $value = (int) $value;
  258. if (isset($_POST[\'karma_bad\']))
  259. {
  260. $profile_vars[\'karma_bad\'] = $_POST[\'karma_bad\'] != \'\' ? (int) $_POST[\'karma_bad\'] : 0;
  261. $cur_profile[\'karma_bad\'] = $_POST[\'karma_bad\'] != \'\' ? (int) $_POST[\'karma_bad\'] : 0;
  262. }
  263. return true;
  264. '),
  265. 'preload' => create_function('', '
  266. global $context, $cur_profile;
  267. $context[\'member\'][\'karma\'][\'good\'] = $cur_profile[\'karma_good\'];
  268. $context[\'member\'][\'karma\'][\'bad\'] = $cur_profile[\'karma_bad\'];
  269. return true;
  270. '),
  271. 'enabled' => !empty($modSettings['karmaMode']),
  272. ),
  273. 'lngfile' => array(
  274. 'type' => 'select',
  275. 'options' => 'return $context[\'profile_languages\'];',
  276. 'label' => $txt['preferred_language'],
  277. 'permission' => 'profile_identity',
  278. 'preload' => 'profileLoadLanguages',
  279. 'enabled' => !empty($modSettings['userLanguage']),
  280. 'value' => empty($cur_profile['lngfile']) ? $language : $cur_profile['lngfile'],
  281. 'input_validate' => create_function('&$value', '
  282. global $context, $cur_profile;
  283. // Load the languages.
  284. profileLoadLanguages();
  285. if (isset($context[\'profile_languages\'][$value]))
  286. {
  287. if ($context[\'user\'][\'is_owner\'] && empty($context[\'password_auth_failed\']))
  288. $_SESSION[\'language\'] = $value;
  289. return true;
  290. }
  291. else
  292. {
  293. $value = $cur_profile[\'lngfile\'];
  294. return false;
  295. }
  296. '),
  297. ),
  298. 'location' => array(
  299. 'type' => 'text',
  300. 'label' => $txt['location'],
  301. 'log_change' => true,
  302. 'size' => 50,
  303. 'permission' => 'profile_extra',
  304. ),
  305. // The username is not always editable - so adjust it as such.
  306. 'member_name' => array(
  307. 'type' => allowedTo('admin_forum') && isset($_GET['changeusername']) ? 'text' : 'label',
  308. 'label' => $txt['username'],
  309. 'subtext' => allowedTo('admin_forum') && !isset($_GET['changeusername']) ? '[<a href="' . $scripturl . '?action=profile;u=' . $context['id_member'] . ';area=account;changeusername" style="font-style: italic;">' . $txt['username_change'] . '</a>]' : '',
  310. 'log_change' => true,
  311. 'permission' => 'profile_identity',
  312. 'prehtml' => allowedTo('admin_forum') && isset($_GET['changeusername']) ? '<div class="alert">' . $txt['username_warning'] . '</div>' : '',
  313. 'input_validate' => create_function('&$value', '
  314. global $sourcedir, $context, $user_info, $cur_profile;
  315. if (allowedTo(\'admin_forum\'))
  316. {
  317. // We\'ll need this...
  318. require_once($sourcedir . \'/Subs-Auth.php\');
  319. // Maybe they are trying to change their password as well?
  320. $resetPassword = true;
  321. if (isset($_POST[\'passwrd1\']) && $_POST[\'passwrd1\'] != \'\' && isset($_POST[\'passwrd2\']) && $_POST[\'passwrd1\'] == $_POST[\'passwrd2\'] && validatePassword($_POST[\'passwrd1\'], $value, array($cur_profile[\'real_name\'], $user_info[\'username\'], $user_info[\'name\'], $user_info[\'email\'])) == null)
  322. $resetPassword = false;
  323. // Do the reset... this will send them an email too.
  324. if ($resetPassword)
  325. resetPassword($context[\'id_member\'], $value);
  326. elseif ($value !== null)
  327. {
  328. validateUsername($context[\'id_member\'], $value);
  329. updateMemberData($context[\'id_member\'], array(\'member_name\' => $value));
  330. }
  331. }
  332. return false;
  333. '),
  334. ),
  335. 'msn' => array(
  336. 'type' => 'text',
  337. 'label' => $txt['msn'],
  338. 'subtext' => $txt['msn_email_address'],
  339. 'size' => 24,
  340. 'permission' => 'profile_extra',
  341. 'input_validate' => create_function('&$value', '
  342. global $cur_profile;
  343. // Make sure the msn one is an email address, not something like \'none\' :P.
  344. if ($value != \'\' && preg_match(\'~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\\\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~\', $value) == 0)
  345. {
  346. $value = $cur_profile[\'msn\'];
  347. return false;
  348. }
  349. return true;
  350. '),
  351. ),
  352. 'passwrd1' => array(
  353. 'type' => 'password',
  354. 'label' => ucwords($txt['choose_pass']),
  355. 'subtext' => $txt['password_strength'],
  356. 'size' => 20,
  357. 'value' => '',
  358. 'enabled' => empty($cur_profile['openid_uri']),
  359. 'permission' => 'profile_identity',
  360. 'save_key' => 'passwd',
  361. // Note this will only work if passwrd2 also exists!
  362. 'input_validate' => create_function('&$value', '
  363. global $sourcedir, $user_info, $smcFunc, $cur_profile;
  364. // If we didn\'t try it then ignore it!
  365. if ($value == \'\')
  366. return false;
  367. // Do the two entries for the password even match?
  368. if (!isset($_POST[\'passwrd2\']) || $value != $_POST[\'passwrd2\'])
  369. return \'bad_new_password\';
  370. // Let\'s get the validation function into play...
  371. require_once($sourcedir . \'/Subs-Auth.php\');
  372. $passwordErrors = validatePassword($value, $cur_profile[\'member_name\'], array($cur_profile[\'real_name\'], $user_info[\'username\'], $user_info[\'name\'], $user_info[\'email\']));
  373. // Were there errors?
  374. if ($passwordErrors != null)
  375. return \'password_\' . $passwordErrors;
  376. // Set up the new password variable... ready for storage.
  377. $value = sha1(strtolower($cur_profile[\'member_name\']) . un_htmlspecialchars($value));
  378. return true;
  379. '),
  380. ),
  381. 'passwrd2' => array(
  382. 'type' => 'password',
  383. 'label' => ucwords($txt['verify_pass']),
  384. 'enabled' => empty($cur_profile['openid_uri']),
  385. 'size' => 20,
  386. 'value' => '',
  387. 'permission' => 'profile_identity',
  388. 'is_dummy' => true,
  389. ),
  390. 'personal_text' => array(
  391. 'type' => 'text',
  392. 'label' => $txt['personal_text'],
  393. 'log_change' => true,
  394. 'input_attr' => array('maxlength="50"'),
  395. 'size' => 50,
  396. 'permission' => 'profile_extra',
  397. ),
  398. // This does ALL the pm settings
  399. 'pm_prefs' => array(
  400. 'type' => 'callback',
  401. 'callback_func' => 'pm_settings',
  402. 'permission' => 'pm_read',
  403. 'preload' => create_function('', '
  404. global $context, $cur_profile;
  405. $context[\'display_mode\'] = $cur_profile[\'pm_prefs\'] & 3;
  406. $context[\'send_email\'] = $cur_profile[\'pm_email_notify\'];
  407. $context[\'receive_from\'] = !empty($cur_profile[\'pm_receive_from\']) ? $cur_profile[\'pm_receive_from\'] : 0;
  408. return true;
  409. '),
  410. 'input_validate' => create_function('&$value', '
  411. global $cur_profile, $profile_vars;
  412. // Simple validate and apply the two "sub settings"
  413. $value = max(min($value, 2), 0);
  414. $cur_profile[\'pm_email_notify\'] = $profile_vars[\'pm_email_notify\'] = max(min((int) $_POST[\'pm_email_notify\'], 2), 0);
  415. $cur_profile[\'pm_receive_from\'] = $profile_vars[\'pm_receive_from\'] = max(min((int) $_POST[\'pm_receive_from\'], 4), 0);
  416. return true;
  417. '),
  418. ),
  419. 'posts' => array(
  420. 'type' => 'int',
  421. 'label' => $txt['profile_posts'],
  422. 'log_change' => true,
  423. 'size' => 7,
  424. 'permission' => 'moderate_forum',
  425. 'input_validate' => create_function('&$value', '
  426. if (!is_numeric($value))
  427. return \'digits_only\';
  428. else
  429. $value = $value != \'\' ? strtr($value, array(\',\' => \'\', \'.\' => \'\', \' \' => \'\')) : 0;
  430. return true;
  431. '),
  432. ),
  433. 'real_name' => array(
  434. 'type' => !empty($modSettings['allow_editDisplayName']) || allowedTo('moderate_forum') ? 'text' : 'label',
  435. 'label' => $txt['name'],
  436. 'subtext' => $txt['display_name_desc'],
  437. 'log_change' => true,
  438. 'input_attr' => array('maxlength="60"'),
  439. 'permission' => 'profile_identity',
  440. 'enabled' => !empty($modSettings['allow_editDisplayName']) || allowedTo('moderate_forum'),
  441. 'input_validate' => create_function('&$value', '
  442. global $context, $smcFunc, $sourcedir, $cur_profile;
  443. $value = trim(preg_replace(\'~[\s]~\' . ($context[\'utf8\'] ? \'u\' : \'\'), \' \', $value));
  444. if (trim($value) == \'\')
  445. return \'no_name\';
  446. elseif ($smcFunc[\'strlen\']($value) > 60)
  447. return \'name_too_long\';
  448. elseif ($cur_profile[\'real_name\'] != $value)
  449. {
  450. require_once($sourcedir . \'/Subs-Members.php\');
  451. if (isReservedName($value, $context[\'id_member\']))
  452. return \'name_taken\';
  453. }
  454. return true;
  455. '),
  456. ),
  457. 'secret_question' => array(
  458. 'type' => 'text',
  459. 'label' => $txt['secret_question'],
  460. 'subtext' => $txt['secret_desc'],
  461. 'size' => 50,
  462. 'permission' => 'profile_identity',
  463. ),
  464. 'secret_answer' => array(
  465. 'type' => 'text',
  466. 'label' => $txt['secret_answer'],
  467. 'subtext' => $txt['secret_desc2'],
  468. 'size' => 20,
  469. 'postinput' => '<span class="smalltext" style="margin-left: 4ex;">[<a href="' . $scripturl . '?action=helpadmin;help=secret_why_blank" onclick="return reqWin(this.href);">' . $txt['secret_why_blank'] . '</a>]</span>',
  470. 'value' => '',
  471. 'permission' => 'profile_identity',
  472. 'input_validate' => create_function('&$value', '
  473. $value = $value != \'\' ? md5($value) : \'\';
  474. return true;
  475. '),
  476. ),
  477. 'signature' => array(
  478. 'type' => 'callback',
  479. 'callback_func' => 'signature_modify',
  480. 'permission' => 'profile_extra',
  481. 'enabled' => substr($modSettings['signature_settings'], 0, 1) == 1,
  482. 'preload' => 'profileLoadSignatureData',
  483. 'input_validate' => 'profileValidateSignature',
  484. ),
  485. 'show_online' => array(
  486. 'type' => 'check',
  487. 'label' => $txt['show_online'],
  488. 'permission' => 'profile_identity',
  489. 'enabled' => !empty($modSettings['allow_hideOnline']) || allowedTo('moderate_forum'),
  490. ),
  491. 'smiley_set' => array(
  492. 'type' => 'callback',
  493. 'callback_func' => 'smiley_pick',
  494. 'enabled' => !empty($modSettings['smiley_sets_enable']),
  495. 'permission' => 'profile_extra',
  496. 'preload' => create_function('', '
  497. global $modSettings, $context, $txt, $cur_profile;
  498. $context[\'member\'][\'smiley_set\'][\'id\'] = empty($cur_profile[\'smiley_set\']) ? \'\' : $cur_profile[\'smiley_set\'];
  499. $context[\'smiley_sets\'] = explode(\',\', \'none,,\' . $modSettings[\'smiley_sets_known\']);
  500. $set_names = explode("\n", $txt[\'smileys_none\'] . "\n" . $txt[\'smileys_forum_board_default\'] . "\n" . $modSettings[\'smiley_sets_names\']);
  501. foreach ($context[\'smiley_sets\'] as $i => $set)
  502. {
  503. $context[\'smiley_sets\'][$i] = array(
  504. \'id\' => htmlspecialchars($set),
  505. \'name\' => htmlspecialchars($set_names[$i]),
  506. \'selected\' => $set == $context[\'member\'][\'smiley_set\'][\'id\']
  507. );
  508. if ($context[\'smiley_sets\'][$i][\'selected\'])
  509. $context[\'member\'][\'smiley_set\'][\'name\'] = $set_names[$i];
  510. }
  511. return true;
  512. '),
  513. 'input_validate' => create_function('&$value', '
  514. global $modSettings;
  515. $smiley_sets = explode(\',\', $modSettings[\'smiley_sets_known\']);
  516. if (!in_array($value, $smiley_sets) && $value != \'none\')
  517. $value = \'\';
  518. return true;
  519. '),
  520. ),
  521. // Pretty much a dummy entry - it populates all the theme settings.
  522. 'theme_settings' => array(
  523. 'type' => 'callback',
  524. 'callback_func' => 'theme_settings',
  525. 'permission' => 'profile_extra',
  526. 'is_dummy' => true,
  527. 'preload' => create_function('', '
  528. global $context, $user_info;
  529. loadLanguage(\'Settings\');
  530. $context[\'allow_no_censored\'] = false;
  531. if ($user_info[\'is_admin\'] || $context[\'user\'][\'is_owner\'])
  532. $context[\'allow_no_censored\'] = allowedTo(\'disable_censor\');
  533. return true;
  534. '),
  535. ),
  536. 'time_format' => array(
  537. 'type' => 'callback',
  538. 'callback_func' => 'timeformat_modify',
  539. 'permission' => 'profile_extra',
  540. 'preload' => create_function('', '
  541. global $context, $user_info, $txt, $cur_profile, $modSettings;
  542. $context[\'easy_timeformats\'] = array(
  543. array(\'format\' => \'\', \'title\' => $txt[\'timeformat_default\']),
  544. array(\'format\' => \'%B %d, %Y, %I:%M:%S %p\', \'title\' => $txt[\'timeformat_easy1\']),
  545. array(\'format\' => \'%B %d, %Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy2\']),
  546. array(\'format\' => \'%Y-%m-%d, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy3\']),
  547. array(\'format\' => \'%d %B %Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy4\']),
  548. array(\'format\' => \'%d-%m-%Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy5\'])
  549. );
  550. $context[\'member\'][\'time_format\'] = $cur_profile[\'time_format\'];
  551. $context[\'current_forum_time\'] = timeformat(time() - $user_info[\'time_offset\'] * 3600, false);
  552. $context[\'current_forum_time_js\'] = strftime(\'%Y,\' . ((int) strftime(\'%m\', time() + $modSettings[\'time_offset\'] * 3600) - 1) . \',%d,%H,%M,%S\', time() + $modSettings[\'time_offset\'] * 3600);
  553. $context[\'current_forum_time_hour\'] = (int) strftime(\'%H\', forum_time(false));
  554. return true;
  555. '),
  556. ),
  557. 'time_offset' => array(
  558. 'type' => 'callback',
  559. 'callback_func' => 'timeoffset_modify',
  560. 'permission' => 'profile_extra',
  561. 'preload' => create_function('', '
  562. global $context, $cur_profile;
  563. $context[\'member\'][\'time_offset\'] = $cur_profile[\'time_offset\'];
  564. return true;
  565. '),
  566. 'input_validate' => create_function('&$value', '
  567. // Validate the time_offset...
  568. $value = (float) strtr($value, \',\', \'.\');
  569. if ($value < -23.5 || $value > 23.5)
  570. return \'bad_offset\';
  571. return true;
  572. '),
  573. ),
  574. 'usertitle' => array(
  575. 'type' => 'text',
  576. 'label' => $txt['custom_title'],
  577. 'log_change' => true,
  578. 'size' => 50,
  579. 'permission' => 'profile_title',
  580. 'enabled' => !empty($modSettings['titlesEnable']),
  581. ),
  582. 'website_title' => array(
  583. 'type' => 'text',
  584. 'label' => $txt['website_title'],
  585. 'subtext' => $txt['include_website_url'],
  586. 'size' => 50,
  587. 'permission' => 'profile_extra',
  588. 'link_with' => 'website',
  589. ),
  590. 'website_url' => array(
  591. 'type' => 'text',
  592. 'label' => $txt['website_url'],
  593. 'subtext' => $txt['complete_url'],
  594. 'size' => 50,
  595. 'permission' => 'profile_extra',
  596. // Fix the URL...
  597. 'input_validate' => create_function('&$value', '
  598. if (strlen(trim($value)) > 0 && strpos($value, \'://\') === false)
  599. $value = \'http://\' . $value;
  600. if (strlen($value) < 8 || (substr($value, 0, 7) !== \'http://\' && substr($value, 0, 8) !== \'https://\'))
  601. $value = \'\';
  602. return true;
  603. '),
  604. 'link_with' => 'website',
  605. ),
  606. 'yim' => array(
  607. 'type' => 'text',
  608. 'label' => $txt['yim'],
  609. 'subtext' => $txt['your_yim'],
  610. 'size' => 24,
  611. 'input_attr' => array('maxlength="32"'),
  612. 'permission' => 'profile_extra',
  613. ),
  614. );
  615. $disabled_fields = !empty($modSettings['disabled_profile_fields']) ? explode(',', $modSettings['disabled_profile_fields']) : array();
  616. // For each of the above let's take out the bits which don't apply - to save memory and security!
  617. foreach ($profile_fields as $key => $field)
  618. {
  619. // Do we have permission to do this?
  620. if (isset($field['permission']) && !allowedTo(($context['user']['is_owner'] ? array($field['permission'] . '_own', $field['permission'] . '_any') : $field['permission'] . '_any')) && !allowedTo($field['permission']))
  621. unset($profile_fields[$key]);
  622. // Is it enabled?
  623. if (isset($field['enabled']) && !$field['enabled'])
  624. unset($profile_fields[$key]);
  625. // Is it specifically disabled?
  626. if (in_array($key, $disabled_fields) || (isset($field['link_with']) && in_array($field['link_with'], $disabled_fields)))
  627. unset($profile_fields[$key]);
  628. }
  629. }
  630. /**
  631. * Setup the context for a page load!
  632. *
  633. * @param array $fields
  634. */
  635. function setupProfileContext($fields)
  636. {
  637. global $profile_fields, $context, $cur_profile, $smcFunc, $txt;
  638. // Make sure we have this!
  639. loadProfileFields(true);
  640. // First check for any linked sets.
  641. foreach ($profile_fields as $key => $field)
  642. if (isset($field['link_with']) && in_array($field['link_with'], $fields))
  643. $fields[] = $key;
  644. // Some default bits.
  645. $context['profile_prehtml'] = '';
  646. $context['profile_posthtml'] = '';
  647. $context['profile_javascript'] = '';
  648. $context['profile_onsubmit_javascript'] = '';
  649. $i = 0;
  650. $last_type = '';
  651. foreach ($fields as $key => $field)
  652. {
  653. if (isset($profile_fields[$field]))
  654. {
  655. // Shortcut.
  656. $cur_field = &$profile_fields[$field];
  657. // Does it have a preload and does that preload succeed?
  658. if (isset($cur_field['preload']) && !$cur_field['preload']())
  659. continue;
  660. // If this is anything but complex we need to do more cleaning!
  661. if ($cur_field['type'] != 'callback' && $cur_field['type'] != 'hidden')
  662. {
  663. if (!isset($cur_field['label']))
  664. $cur_field['label'] = isset($txt[$field]) ? $txt[$field] : $field;
  665. // Everything has a value!
  666. if (!isset($cur_field['value']))
  667. {
  668. $cur_field['value'] = isset($cur_profile[$field]) ? $cur_profile[$field] : '';
  669. }
  670. // Any input attributes?
  671. $cur_field['input_attr'] = !empty($cur_field['input_attr']) ? implode(',', $cur_field['input_attr']) : '';
  672. }
  673. // Was there an error with this field on posting?
  674. if (isset($context['profile_errors'][$field]))
  675. $cur_field['is_error'] = true;
  676. // Any javascript stuff?
  677. if (!empty($cur_field['js_submit']))
  678. $context['profile_onsubmit_javascript'] .= $cur_field['js_submit'];
  679. if (!empty($cur_field['js']))
  680. $context['profile_javascript'] .= $cur_field['js'];
  681. // Any template stuff?
  682. if (!empty($cur_field['prehtml']))
  683. $context['profile_prehtml'] .= $cur_field['prehtml'];
  684. if (!empty($cur_field['posthtml']))
  685. $context['profile_posthtml'] .= $cur_field['posthtml'];
  686. // Finally put it into context?
  687. if ($cur_field['type'] != 'hidden')
  688. {
  689. $last_type = $cur_field['type'];
  690. $context['profile_fields'][$field] = &$profile_fields[$field];
  691. }
  692. }
  693. // Bodge in a line break - without doing two in a row ;)
  694. elseif ($field == 'hr' && $last_type != 'hr' && $last_type != '')
  695. {
  696. $last_type = 'hr';
  697. $context['profile_fields'][$i++]['type'] = 'hr';
  698. }
  699. }
  700. // Free up some memory.
  701. unset($profile_fields);
  702. }
  703. /**
  704. * Save the profile changes.
  705. */
  706. function saveProfileFields()
  707. {
  708. global $profile_fields, $profile_vars, $context, $old_profile, $post_errors, $sourcedir, $modSettings, $cur_profile, $smcFunc;
  709. // Load them up.
  710. loadProfileFields();
  711. // This makes things easier...
  712. $old_profile = $cur_profile;
  713. // This allows variables to call activities when they save - by default just to reload their settings
  714. $context['profile_execute_on_save'] = array();
  715. if ($context['user']['is_owner'])
  716. $context['profile_execute_on_save']['reload_user'] = 'profileReloadUser';
  717. // Assume we log nothing.
  718. $context['log_changes'] = array();
  719. // Cycle through the profile fields working out what to do!
  720. foreach ($profile_fields as $key => $field)
  721. {
  722. if (!isset($_POST[$key]) || !empty($field['is_dummy']))
  723. continue;
  724. // What gets updated?
  725. $db_key = isset($field['save_key']) ? $field['save_key'] : $key;
  726. // Right - we have something that is enabled, we can act upon and has a value posted to it. Does it have a validation function?
  727. if (isset($field['input_validate']))
  728. {
  729. $is_valid = $field['input_validate']($_POST[$key]);
  730. // An error occured - set it as such!
  731. if ($is_valid !== true)
  732. {
  733. // Is this an actual error?
  734. if ($is_valid !== false)
  735. {
  736. $post_errors[$key] = $is_valid;
  737. $profile_fields[$key]['is_error'] = $is_valid;
  738. }
  739. // Retain the old value.
  740. $cur_profile[$key] = $_POST[$key];
  741. continue;
  742. }
  743. }
  744. // Are we doing a cast?
  745. $field['cast_type'] = empty($field['cast_type']) ? $field['type'] : $field['cast_type'];
  746. // Finally, clean up certain types.
  747. if ($field['cast_type'] == 'int')
  748. $_POST[$key] = (int) $_POST[$key];
  749. elseif ($field['cast_type'] == 'float')
  750. $_POST[$key] = (float) $_POST[$key];
  751. elseif ($field['cast_type'] == 'check')
  752. $_POST[$key] = !empty($_POST[$key]) ? 1 : 0;
  753. // If we got here we're doing OK.
  754. if ($field['type'] != 'hidden' && (!isset($old_profile[$key]) || $_POST[$key] != $old_profile[$key]))
  755. {
  756. // Set the save variable.
  757. $profile_vars[$db_key] = $_POST[$key];
  758. // And update the user profile.
  759. $cur_profile[$key] = $_POST[$key];
  760. // Are we logging it?
  761. if (!empty($field['log_change']) && isset($old_profile[$key]))
  762. $context['log_changes'][$key] = array(
  763. 'previous' => $old_profile[$key],
  764. 'new' => $_POST[$key],
  765. );
  766. }
  767. // Logging group changes are a bit different...
  768. if ($key == 'id_group' && $field['log_change'])
  769. {
  770. profileLoadGroups();
  771. // Any changes to primary group?
  772. if ($_POST['id_group'] != $old_profile['id_group'])
  773. {
  774. $context['log_changes']['id_group'] = array(
  775. 'previous' => !empty($old_profile[$key]) && isset($context['member_groups'][$old_profile[$key]]) ? $context['member_groups'][$old_profile[$key]]['name'] : '',
  776. 'new' => !empty($_POST[$key]) && isset($context['member_groups'][$_POST[$key]]) ? $context['member_groups'][$_POST[$key]]['name'] : '',
  777. );
  778. }
  779. // Prepare additional groups for comparison.
  780. $additional_groups = array(
  781. 'previous' => !empty($old_profile['additional_groups']) ? explode(',', $old_profile['additional_groups']) : array(),
  782. 'new' => !empty($_POST['additional_groups']) ? array_diff($_POST['additional_groups'], array(0)) : array(),
  783. );
  784. sort($additional_groups['previous']);
  785. sort($additional_groups['new']);
  786. // What about additional groups?
  787. if ($additional_groups['previous'] != $additional_groups['new'])
  788. {
  789. foreach ($additional_groups as $type => $groups)
  790. {
  791. foreach ($groups as $id => $group)
  792. {
  793. if (isset($context['member_groups'][$group]))
  794. $additional_groups[$type][$id] = $context['member_groups'][$group]['name'];
  795. else
  796. unset($additional_groups[$type][$id]);
  797. }
  798. $additional_groups[$type] = implode(', ', $additional_groups[$type]);
  799. }
  800. $context['log_changes']['additional_groups'] = $additional_groups;
  801. }
  802. }
  803. }
  804. // @todo Temporary
  805. if ($context['user']['is_owner'])
  806. $changeOther = allowedTo(array('profile_extra_any', 'profile_extra_own'));
  807. else
  808. $changeOther = allowedTo('profile_extra_any');
  809. if ($changeOther && empty($post_errors))
  810. {
  811. makeThemeChanges($context['id_member'], isset($_POST['id_theme']) ? (int) $_POST['id_theme'] : $old_profile['id_theme']);
  812. if (!empty($_REQUEST['sa']))
  813. makeCustomFieldChanges($context['id_member'], $_REQUEST['sa'], false);
  814. }
  815. // Free memory!
  816. unset($profile_fields);
  817. }
  818. /**
  819. * Save the profile changes
  820. *
  821. * @param array &$profile_variables
  822. * @param array &$post_errors
  823. * @param int $memID id_member
  824. */
  825. function saveProfileChanges(&$profile_vars, &$post_errors, $memID)
  826. {
  827. global $user_info, $txt, $modSettings, $user_profile;
  828. global $context, $settings, $sourcedir;
  829. global $smcFunc;
  830. // These make life easier....
  831. $old_profile = &$user_profile[$memID];
  832. // Permissions...
  833. if ($context['user']['is_owner'])
  834. {
  835. $changeIdentity = allowedTo(array('profile_identity_any', 'profile_identity_own'));
  836. $changeOther = allowedTo(array('profile_extra_any', 'profile_extra_own'));
  837. }
  838. else
  839. {
  840. $changeIdentity = allowedTo('profile_identity_any');
  841. $changeOther = allowedTo('profile_extra_any');
  842. }
  843. // Arrays of all the changes - makes things easier.
  844. $profile_bools = array(
  845. 'notify_announcements', 'notify_send_body',
  846. );
  847. $profile_ints = array(
  848. 'notify_regularity',
  849. 'notify_types',
  850. );
  851. $profile_floats = array(
  852. );
  853. $profile_strings = array(
  854. 'buddy_list',
  855. 'ignore_boards',
  856. );
  857. if (isset($_POST['sa']) && $_POST['sa'] == 'ignoreboards' && empty($_POST['ignore_brd']))
  858. $_POST['ignore_brd'] = array();
  859. unset($_POST['ignore_boards']); // Whatever it is set to is a dirty fithy thing. Kinda like our minds.
  860. if (isset($_POST['ignore_brd']))
  861. {
  862. if (!is_array($_POST['ignore_brd']))
  863. $_POST['ignore_brd'] = array ($_POST['ignore_brd']);
  864. foreach ($_POST['ignore_brd'] as $k => $d)
  865. {
  866. $d = (int) $d;
  867. if ($d != 0)
  868. $_POST['ignore_brd'][$k] = $d;
  869. else
  870. unset($_POST['ignore_brd'][$k]);
  871. }
  872. $_POST['ignore_boards'] = implode(',', $_POST['ignore_brd']);
  873. unset($_POST['ignore_brd']);
  874. }
  875. // Here's where we sort out all the 'other' values...
  876. if ($changeOther)
  877. {
  878. makeThemeChanges($memID, isset($_POST['id_theme']) ? (int) $_POST['id_theme'] : $old_profile['id_theme']);
  879. //makeAvatarChanges($memID, $post_errors);
  880. makeNotificationChanges($memID);
  881. if (!empty($_REQUEST['sa']))
  882. makeCustomFieldChanges($memID, $_REQUEST['sa'], false);
  883. foreach ($profile_bools as $var)
  884. if (isset($_POST[$var]))
  885. $profile_vars[$var] = empty($_POST[$var]) ? '0' : '1';
  886. foreach ($profile_ints as $var)
  887. if (isset($_POST[$var]))
  888. $profile_vars[$var] = $_POST[$var] != '' ? (int) $_POST[$var] : '';
  889. foreach ($profile_floats as $var)
  890. if (isset($_POST[$var]))
  891. $profile_vars[$var] = (float) $_POST[$var];
  892. foreach ($profile_strings as $var)
  893. if (isset($_POST[$var]))
  894. $profile_vars[$var] = $_POST[$var];
  895. }
  896. }
  897. /**
  898. * Make any theme changes that are sent with the profile.
  899. *
  900. * @param int $memID
  901. * @param int $id_theme
  902. */
  903. function makeThemeChanges($memID, $id_theme)
  904. {
  905. global $modSettings, $smcFunc, $context, $user_info;
  906. $reservedVars = array(
  907. 'actual_theme_url',
  908. 'actual_images_url',
  909. 'base_theme_dir',
  910. 'base_theme_url',
  911. 'default_images_url',
  912. 'default_theme_dir',
  913. 'default_theme_url',
  914. 'default_template',
  915. 'images_url',
  916. 'number_recent_posts',
  917. 'smiley_sets_default',
  918. 'theme_dir',
  919. 'theme_id',
  920. 'theme_layers',
  921. 'theme_templates',
  922. 'theme_url',
  923. );
  924. // Can't change reserved vars.
  925. if ((isset($_POST['options']) && count(array_intersect(array_keys($_POST['options']), $reservedVars)) != 0) || (isset($_POST['default_options']) && count(array_intersect(array_keys($_POST['default_options']), $reservedVars)) != 0))
  926. fatal_lang_error('no_access', false);
  927. // Don't allow any overriding of custom fields with default or non-default options.
  928. $request = $smcFunc['db_query']('', '
  929. SELECT col_name
  930. FROM {db_prefix}custom_fields
  931. WHERE active = {int:is_active}',
  932. array(
  933. 'is_active' => 1,
  934. )
  935. );
  936. $custom_fields = array();
  937. while ($row = $smcFunc['db_fetch_assoc']($request))
  938. $custom_fields[] = $row['col_name'];
  939. $smcFunc['db_free_result']($request);
  940. // These are the theme changes...
  941. $themeSetArray = array();
  942. if (isset($_POST['options']) && is_array($_POST['options']))
  943. {
  944. foreach ($_POST['options'] as $opt => $val)
  945. {
  946. if (in_array($opt, $custom_fields))
  947. continue;
  948. // These need to be controlled.
  949. if ($opt == 'topics_per_page' || $opt == 'messages_per_page')
  950. $val = max(0, min($val, 50));
  951. // We don't set this per theme anymore.
  952. elseif ($opt == 'allow_no_censored')
  953. continue;
  954. $themeSetArray[] = array($memID, $id_theme, $opt, is_array($val) ? implode(',', $val) : $val);
  955. }
  956. }
  957. $erase_options = array();
  958. if (isset($_POST['default_options']) && is_array($_POST['default_options']))
  959. foreach ($_POST['default_options'] as $opt => $val)
  960. {
  961. if (in_array($opt, $custom_fields))
  962. continue;
  963. // These need to be controlled.
  964. if ($opt == 'topics_per_page' || $opt == 'messages_per_page')
  965. $val = max(0, min($val, 50));
  966. // Only let admins and owners change the censor.
  967. elseif ($opt == 'allow_no_censored' && !$user_info['is_admin'] && !$context['user']['is_owner'])
  968. continue;
  969. $themeSetArray[] = array($memID, 1, $opt, is_array($val) ? implode(',', $val) : $val);
  970. $erase_options[] = $opt;
  971. }
  972. // If themeSetArray isn't still empty, send it to the database.
  973. if (empty($context['password_auth_failed']))
  974. {
  975. if (!empty($themeSetArray))
  976. {
  977. $smcFunc['db_insert']('replace',
  978. '{db_prefix}themes',
  979. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  980. $themeSetArray,
  981. array('id_member', 'id_theme', 'variable')
  982. );
  983. }
  984. if (!empty($erase_options))
  985. {
  986. $smcFunc['db_query']('', '
  987. DELETE FROM {db_prefix}themes
  988. WHERE id_theme != {int:id_theme}
  989. AND variable IN ({array_string:erase_variables})
  990. AND id_member = {int:id_member}',
  991. array(
  992. 'id_theme' => 1,
  993. 'id_member' => $memID,
  994. 'erase_variables' => $erase_options
  995. )
  996. );
  997. }
  998. $themes = explode(',', $modSettings['knownThemes']);
  999. foreach ($themes as $t)
  1000. cache_put_data('theme_settings-' . $t . ':' . $memID, null, 60);
  1001. }
  1002. }
  1003. /**
  1004. * Make any notification changes that need to be made.
  1005. *
  1006. * @param int $memID id_member
  1007. */
  1008. function makeNotificationChanges($memID)
  1009. {
  1010. global $smcFunc;
  1011. // Update the boards they are being notified on.
  1012. if (isset($_POST['edit_notify_boards']) && !empty($_POST['notify_boards']))
  1013. {
  1014. // Make sure only integers are deleted.
  1015. foreach ($_POST['notify_boards'] as $index => $id)
  1016. $_POST['notify_boards'][$index] = (int) $id;
  1017. // id_board = 0 is reserved for topic notifications.
  1018. $_POST['notify_boards'] = array_diff($_POST['notify_boards'], array(0));
  1019. $smcFunc['db_query']('', '
  1020. DELETE FROM {db_prefix}log_notify
  1021. WHERE id_board IN ({array_int:board_list})
  1022. AND id_member = {int:selected_member}',
  1023. array(
  1024. 'board_list' => $_POST['notify_boards'],
  1025. 'selected_member' => $memID,
  1026. )
  1027. );
  1028. }
  1029. // We are editing topic notifications......
  1030. elseif (isset($_POST['edit_notify_topics']) && !empty($_POST['notify_topics']))
  1031. {
  1032. foreach ($_POST['notify_topics'] as $index => $id)
  1033. $_POST['notify_topics'][$index] = (int) $id;
  1034. // Make sure there are no zeros left.
  1035. $_POST['notify_topics'] = array_diff($_POST['notify_topics'], array(0));
  1036. $smcFunc['db_query']('', '
  1037. DELETE FROM {db_prefix}log_notify
  1038. WHERE id_topic IN ({array_int:topic_list})
  1039. AND id_member = {int:selected_member}',
  1040. array(
  1041. 'topic_list' => $_POST['notify_topics'],
  1042. 'selected_member' => $memID,
  1043. )
  1044. );
  1045. }
  1046. }
  1047. /**
  1048. * Save any changes to the custom profile fields
  1049. *
  1050. * @param int $memID
  1051. * @param string $area
  1052. * @param bool $sanitize = true
  1053. */
  1054. function makeCustomFieldChanges($memID, $area, $sanitize = true)
  1055. {
  1056. global $context, $smcFunc, $user_profile, $user_info, $modSettings, $sourcedir;
  1057. if ($sanitize && isset($_POST['customfield']))
  1058. $_POST['customfield'] = htmlspecialchars__recursive($_POST['customfield']);
  1059. $where = $area == 'register' ? 'show_reg != 0' : 'show_profile = {string:area}';
  1060. // Load the fields we are saving too - make sure we save valid data (etc).
  1061. $request = $smcFunc['db_query']('', '
  1062. SELECT col_name, field_name, field_desc, field_type, field_length, field_options, default_value, show_reg, mask, private
  1063. FROM {db_prefix}custom_fields
  1064. WHERE ' . $where . '
  1065. AND active = {int:is_active}',
  1066. array(
  1067. 'is_active' => 1,
  1068. 'area' => $area,
  1069. )
  1070. );
  1071. $changes = array();
  1072. $log_changes = array();
  1073. while ($row = $smcFunc['db_fetch_assoc']($request))
  1074. {
  1075. /* This means don't save if:
  1076. - The user is NOT an admin.
  1077. - The data is not freely viewable and editable by users.
  1078. - The data is not invisible to users but editable by the owner (or if it is the user is not the owner)
  1079. - The area isn't registration, and if it is that the field is not suppossed to be shown there.
  1080. */
  1081. if ($row['private'] != 0 && !allowedTo('admin_forum') && ($memID != $user_info['id'] || $row['private'] != 2) && ($area != 'register' || $row['show_reg'] == 0))
  1082. continue;
  1083. // Validate the user data.
  1084. if ($row['field_type'] == 'check')
  1085. $value = isset($_POST['customfield'][$row['col_name']]) ? 1 : 0;
  1086. elseif ($row['field_type'] == 'select' || $row['field_type'] == 'radio')
  1087. {
  1088. $value = $row['default_value'];
  1089. foreach (explode(',', $row['field_options']) as $k => $v)
  1090. if (isset($_POST['customfield'][$row['col_name']]) && $_POST['customfield'][$row['col_name']] == $k)
  1091. $value = $v;
  1092. }
  1093. // Otherwise some form of text!
  1094. else
  1095. {
  1096. $value = isset($_POST['customfield'][$row['col_name']]) ? $_POST['customfield'][$row['col_name']] : '';
  1097. if ($row['field_length'])
  1098. $value = $smcFunc['substr']($value, 0, $row['field_length']);
  1099. // Any masks?
  1100. if ($row['field_type'] == 'text' && !empty($row['mask']) && $row['mask'] != 'none')
  1101. {
  1102. // @todo We never error on this - just ignore it at the moment...
  1103. if ($row['mask'] == 'email' && (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $value) === 0 || strlen($value) > 255))
  1104. $value = '';
  1105. elseif ($row['mask'] == 'number')
  1106. {
  1107. $value = (int) $value;
  1108. }
  1109. elseif (substr($row['mask'], 0, 5) == 'regex' && preg_match(substr($row['mask'], 5), $value) === 0)
  1110. $value = '';
  1111. }
  1112. }
  1113. // Did it change?
  1114. if (!isset($user_profile[$memID]['options'][$row['col_name']]) || $user_profile[$memID]['options'][$row['col_name']] != $value)
  1115. {
  1116. $log_changes[] = array(
  1117. 'action' => 'customfield_' . $row['col_name'],
  1118. 'log_type' => 'user',
  1119. 'extra' => array(
  1120. 'previous' => !empty($user_profile[$memID]['options'][$row['col_name']]) ? $user_profile[$memID]['options'][$row['col_name']] : '',
  1121. 'new' => $value,
  1122. 'applicator' => $user_info['id'],
  1123. 'member_affected' => $memID,
  1124. ),
  1125. );
  1126. $changes[] = array(1, $row['col_name'], $value, $memID);
  1127. $user_profile[$memID]['options'][$row['col_name']] = $value;
  1128. }
  1129. }
  1130. $smcFunc['db_free_result']($request);
  1131. // Make those changes!
  1132. if (!empty($changes) && empty($context['password_auth_failed']))
  1133. {
  1134. $smcFunc['db_insert']('replace',
  1135. '{db_prefix}themes',
  1136. array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534', 'id_member' => 'int'),
  1137. $changes,
  1138. array('id_theme', 'variable', 'id_member')
  1139. );
  1140. if (!empty($log_changes) && !empty($modSettings['modlog_enabled']))
  1141. {
  1142. require_once($sourcedir . '/Logging.php');
  1143. foreach ($log_changes as $log_change)
  1144. logAction($log_change['action'], $log_change['extra'], $log_change['log_type']);
  1145. }
  1146. }
  1147. }
  1148. /**
  1149. * Show all the users buddies, as well as a add/delete interface.
  1150. *
  1151. * @param int $memID id_member
  1152. */
  1153. function editBuddyIgnoreLists($memID)
  1154. {
  1155. global $sourcedir, $context, $txt, $scripturl, $modSettings, $user_profile;
  1156. // Do a quick check to ensure people aren't getting here illegally!
  1157. if (!$context['user']['is_owner'] || empty($modSettings['enable_buddylist']))
  1158. fatal_lang_error('no_access', false);
  1159. // Can we email the user direct?
  1160. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  1161. $subActions = array(
  1162. 'buddies' => array('editBuddies', $txt['editBuddies']),
  1163. 'ignore' => array('editIgnoreList', $txt['editIgnoreList']),
  1164. );
  1165. $context['list_area'] = isset($_GET['sa']) && isset($subActions[$_GET['sa']]) ? $_GET['sa'] : 'buddies';
  1166. // Create the tabs for the template.
  1167. $context[$context['profile_menu_name']]['tab_data'] = array(
  1168. 'title' => $txt['editBuddyIgnoreLists'],
  1169. 'description' => $txt['buddy_ignore_desc'],
  1170. 'icon' => 'profile_sm.png',
  1171. 'tabs' => array(
  1172. 'buddies' => array(),
  1173. 'ignore' => array(),
  1174. ),
  1175. );
  1176. // Pass on to the actual function.
  1177. $context['sub_template'] = $subActions[$context['list_area']][0];
  1178. $subActions[$context['list_area']][0]($memID);
  1179. }
  1180. /**
  1181. * Show all the users buddies, as well as a add/delete interface.
  1182. *
  1183. * @param int $memID id_member
  1184. */
  1185. function editBuddies($memID)
  1186. {
  1187. global $txt, $scripturl, $modSettings;
  1188. global $context, $user_profile, $memberContext, $smcFunc;
  1189. // For making changes!
  1190. $buddiesArray = explode(',', $user_profile[$memID]['buddy_list']);
  1191. foreach ($buddiesArray as $k => $dummy)
  1192. if ($dummy == '')
  1193. unset($buddiesArray[$k]);
  1194. // Removing a buddy?
  1195. if (isset($_GET['remove']))
  1196. {
  1197. checkSession('get');
  1198. // Heh, I'm lazy, do it the easy way...
  1199. foreach ($buddiesArray as $key => $buddy)
  1200. if ($buddy == (int) $_GET['remove'])
  1201. unset($buddiesArray[$key]);
  1202. // Make the changes.
  1203. $user_profile[$memID]['buddy_list'] = implode(',', $buddiesArray);
  1204. updateMemberData($memID, array('buddy_list' => $user_profile[$memID]['buddy_list']));
  1205. // Redirect off the page because we don't like all this ugly query stuff to stick in the history.
  1206. redirectexit('action=profile;area=lists;sa=buddies;u=' . $memID);
  1207. }
  1208. elseif (isset($_POST['new_buddy']))
  1209. {
  1210. checkSession();
  1211. // Prepare the string for extraction...
  1212. $_POST['new_buddy'] = strtr($smcFunc['htmlspecialchars']($_POST['new_buddy'], ENT_QUOTES), array('&quot;' => '"'));
  1213. preg_match_all('~"([^"]+)"~', $_POST['new_buddy'], $matches);
  1214. $new_buddies = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_POST['new_buddy']))));
  1215. foreach ($new_buddies as $k => $dummy)
  1216. {
  1217. $new_buddies[$k] = strtr(trim($new_buddies[$k]), array('\'' => '&#039;'));
  1218. if (strlen($new_buddies[$k]) == 0 || in_array($new_buddies[$k], array($user_profile[$memID]['member_name'], $user_profile[$memID]['real_name'])))
  1219. unset($new_buddies[$k]);
  1220. }
  1221. if (!empty($new_buddies))
  1222. {
  1223. // Now find out the id_member of the buddy.
  1224. $request = $smcFunc['db_query']('', '
  1225. SELECT id_member
  1226. FROM {db_prefix}members
  1227. WHERE member_name IN ({array_string:new_buddies}) OR real_name IN ({array_string:new_buddies})
  1228. LIMIT {int:count_new_buddies}',
  1229. array(
  1230. 'new_buddies' => $new_buddies,
  1231. 'count_new_buddies' => count($new_buddies),
  1232. )
  1233. );
  1234. // Add the new member to the buddies array.
  1235. while ($row = $smcFunc['db_fetch_assoc']($request))
  1236. $buddiesArray[] = (int) $row['id_member'];
  1237. $smcFunc['db_free_result']($request);
  1238. // Now update the current users buddy list.
  1239. $user_profile[$memID]['buddy_list'] = implode(',', $buddiesArray);
  1240. updateMemberData($memID, array('buddy_list' => $user_profile[$memID]['buddy_list']));
  1241. }
  1242. // Back to the buddy list!
  1243. redirectexit('action=profile;area=lists;sa=buddies;u=' . $memID);
  1244. }
  1245. // Get all the users "buddies"...
  1246. $buddies = array();
  1247. if (!empty($buddiesArray))
  1248. {
  1249. $result = $smcFunc['db_query']('', '
  1250. SELECT id_member
  1251. FROM {db_prefix}members
  1252. WHERE id_member IN ({array_int:buddy_list})
  1253. ORDER BY real_name
  1254. LIMIT {int:buddy_list_count}',
  1255. array(
  1256. 'buddy_list' => $buddiesArray,
  1257. 'buddy_list_count' => substr_count($user_profile[$memID]['buddy_list'], ',') + 1,
  1258. )
  1259. );
  1260. while ($row = $smcFunc['db_fetch_assoc']($result))
  1261. $buddies[] = $row['id_member'];
  1262. $smcFunc['db_free_result']($result);
  1263. }
  1264. $context['buddy_count'] = count($buddies);
  1265. // Load all the members up.
  1266. loadMemberData($buddies, false, 'profile');
  1267. // Setup the context for each buddy.
  1268. $context['buddies'] = array();
  1269. foreach ($buddies as $buddy)
  1270. {
  1271. loadMemberContext($buddy);
  1272. $context['buddies'][$buddy] = $memberContext[$buddy];
  1273. }
  1274. }
  1275. /**
  1276. * Allows the user to view their ignore list, as well as the option to manage members on it.
  1277. *
  1278. * @param int $memID id_member
  1279. */
  1280. function editIgnoreList($memID)
  1281. {
  1282. global $txt, $scripturl, $modSettings;
  1283. global $context, $user_profile, $memberContext, $smcFunc;
  1284. // For making changes!
  1285. $ignoreArray = explode(',', $user_profile[$memID]['pm_ignore_list']);
  1286. foreach ($ignoreArray as $k => $dummy)
  1287. if ($dummy == '')
  1288. unset($ignoreArray[$k]);
  1289. // Removing a member from the ignore list?
  1290. if (isset($_GET['remove']))
  1291. {
  1292. checkSession('get');
  1293. // Heh, I'm lazy, do it the easy way...
  1294. foreach ($ignoreArray as $key => $id_remove)
  1295. if ($id_remove == (int) $_GET['remove'])
  1296. unset($ignoreArray[$key]);
  1297. // Make the changes.
  1298. $user_profile[$memID]['pm_ignore_list'] = implode(',', $ignoreArray);
  1299. updateMemberData($memID, array('pm_ignore_list' => $user_profile[$memID]['pm_ignore_list']));
  1300. // Redirect off the page because we don't like all this ugly query stuff to stick in the history.
  1301. redirectexit('action=profile;area=lists;sa=ignore;u=' . $memID);
  1302. }
  1303. elseif (isset($_POST['new_ignore']))
  1304. {
  1305. checkSession();
  1306. // Prepare the string for extraction...
  1307. $_POST['new_ignore'] = strtr($smcFunc['htmlspecialchars']($_POST['new_ignore'], ENT_QUOTES), array('&quot;' => '"'));
  1308. preg_match_all('~"([^"]+)"~', $_POST['new_ignore'], $matches);
  1309. $new_entries = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_POST['new_ignore']))));
  1310. foreach ($new_entries as $k => $dummy)
  1311. {
  1312. $new_entries[$k] = strtr(trim($new_entries[$k]), array('\'' => '&#039;'));
  1313. if (strlen($new_entries[$k]) == 0 || in_array($new_entries[$k], array($user_profile[$memID]['member_name'], $user_profile[$memID]['real_name'])))
  1314. unset($new_entries[$k]);
  1315. }
  1316. if (!empty($new_entries))
  1317. {
  1318. // Now find out the id_member for the members in question.
  1319. $request = $smcFunc['db_query']('', '
  1320. SELECT id_member
  1321. FROM {db_prefix}members
  1322. WHERE member_name IN ({array_string:new_entries}) OR real_name IN ({array_string:new_entries})
  1323. LIMIT {int:count_new_entries}',
  1324. array(
  1325. 'new_entries' => $new_entries,
  1326. 'count_new_entries' => count($new_entries),
  1327. )
  1328. );
  1329. // Add the new member to the buddies array.
  1330. while ($row = $smcFunc['db_fetch_assoc']($request))
  1331. $ignoreArray[] = (int) $row['id_member'];
  1332. $smcFunc['db_free_result']($request);
  1333. // Now update the current users buddy list.
  1334. $user_profile[$memID]['pm_ignore_list'] = implode(',', $ignoreArray);
  1335. updateMemberData($memID, array('pm_ignore_list' => $user_profile[$memID]['pm_ignore_list']));
  1336. }
  1337. // Back to the list of pityful people!
  1338. redirectexit('action=profile;area=lists;sa=ignore;u=' . $memID);
  1339. }
  1340. // Initialise the list of members we're ignoring.
  1341. $ignored = array();
  1342. if (!empty($ignoreArray))
  1343. {
  1344. $result = $smcFunc['db_query']('', '
  1345. SELECT id_member
  1346. FROM {db_prefix}members
  1347. WHERE id_member IN ({array_int:ignore_list})
  1348. ORDER BY real_name
  1349. LIMIT {int:ignore_list_count}',
  1350. array(
  1351. 'ignore_list' => $ignoreArray,
  1352. 'ignore_list_count' => substr_count($user_profile[$memID]['pm_ignore_list'], ',') + 1,
  1353. )
  1354. );
  1355. while ($row = $smcFunc['db_fetch_assoc']($result))
  1356. $ignored[] = $row['id_member'];
  1357. $smcFunc['db_free_result']($result);
  1358. }
  1359. $context['ignore_count'] = count($ignored);
  1360. // Load all the members up.
  1361. loadMemberData($ignored, false, 'profile');
  1362. // Setup the context for each buddy.
  1363. $context['ignore_list'] = array();
  1364. foreach ($ignored as $ignore_member)
  1365. {
  1366. loadMemberContext($ignore_member);
  1367. $context['ignore_list'][$ignore_member] = $memberContext[$ignore_member];
  1368. }
  1369. }
  1370. /**
  1371. * @todo Needs a description
  1372. *
  1373. * @param int $memID id_member
  1374. */
  1375. function account($memID)
  1376. {
  1377. global $context, $txt;
  1378. loadThemeOptions($memID);
  1379. if (allowedTo(array('profile_identity_own', 'profile_identity_any')))
  1380. loadCustomFields($memID, 'account');
  1381. $context['sub_template'] = 'edit_options';
  1382. $context['page_desc'] = $txt['account_info'];
  1383. setupProfileContext(
  1384. array(
  1385. 'member_name', 'real_name', 'date_registered', 'posts', 'lngfile', 'hr',
  1386. 'id_group', 'hr',
  1387. 'email_address', 'hide_email', 'show_online', 'hr',
  1388. 'passwrd1', 'passwrd2', 'hr',
  1389. 'secret_question', 'secret_answer',
  1390. )
  1391. );
  1392. }
  1393. /**
  1394. * @todo Needs a description
  1395. *
  1396. * @param int $memID id_member
  1397. */
  1398. function forumProfile($memID)
  1399. {
  1400. global $context, $user_profile, $user_info, $txt, $modSettings;
  1401. loadThemeOptions($memID);
  1402. if (allowedTo(array('profile_extra_own', 'profile_extra_any')))
  1403. loadCustomFields($memID, 'forumprofile');
  1404. $context['sub_template'] = 'edit_options';
  1405. $context['page_desc'] = $txt['forumProfile_info'];
  1406. setupProfileContext(
  1407. array(
  1408. 'avatar_choice', 'hr', 'personal_text', 'hr',
  1409. 'bday1', 'location', 'gender', 'hr',
  1410. 'icq', 'aim', 'msn', 'yim', 'hr',
  1411. 'usertitle', 'signature', 'hr',
  1412. 'karma_good', 'hr',
  1413. 'website_title', 'website_url',
  1414. )
  1415. );
  1416. }
  1417. /**
  1418. * Allow the edit of *someone elses* personal message settings.
  1419. *
  1420. * @param int $memID id_member
  1421. */
  1422. function pmprefs($memID)
  1423. {
  1424. global $sourcedir, $context, $txt, $scripturl;
  1425. loadThemeOptions($memID);
  1426. loadCustomFields($memID, 'pmprefs');
  1427. $context['sub_template'] = 'edit_options';
  1428. $context['page_desc'] = $txt['pm_settings_desc'];
  1429. setupProfileContext(
  1430. array(
  1431. 'pm_prefs',
  1432. )
  1433. );
  1434. }
  1435. /**
  1436. * Recursive function to retrieve server-stored avatar files
  1437. *
  1438. * @param string $directory
  1439. * @param int $level
  1440. * @return array
  1441. */
  1442. function getAvatars($directory, $level)
  1443. {
  1444. global $context, $txt, $modSettings;
  1445. $result = array();
  1446. // Open the directory..
  1447. $dir = dir($modSettings['avatar_directory'] . (!empty($directory) ? '/' : '') . $directory);
  1448. $dirs = array();
  1449. $files = array();
  1450. if (!$dir)
  1451. return array();
  1452. while ($line = $dir->read())
  1453. {
  1454. if (in_array($line, array('.', '..', 'blank.png', 'index.php')))
  1455. continue;
  1456. if (is_dir($modSettings['avatar_directory'] . '/' . $directory . (!empty($directory) ? '/' : '') . $line))
  1457. $dirs[] = $line;
  1458. else
  1459. $files[] = $line;
  1460. }
  1461. $dir->close();
  1462. // Sort the results...
  1463. natcasesort($dirs);
  1464. natcasesort($files);
  1465. if ($level == 0)
  1466. {
  1467. $result[] = array(
  1468. 'filename' => 'blank.png',
  1469. 'checked' => in_array($context['member']['avatar']['server_pic'], array('', 'blank.png')),
  1470. 'name' => $txt['no_pic'],
  1471. 'is_dir' => false
  1472. );
  1473. }
  1474. foreach ($dirs as $line)
  1475. {
  1476. $tmp = getAvatars($directory . (!empty($directory) ? '/' : '') . $line, $level + 1);
  1477. if (!empty($tmp))
  1478. $result[] = array(
  1479. 'filename' => htmlspecialchars($line),
  1480. 'checked' => strpos($context['member']['avatar']['server_pic'], $line . '/') !== false,
  1481. 'name' => '[' . htmlspecialchars(str_replace('_', ' ', $line)) . ']',
  1482. 'is_dir' => true,
  1483. 'files' => $tmp
  1484. );
  1485. unset($tmp);
  1486. }
  1487. foreach ($files as $line)
  1488. {
  1489. $filename = substr($line, 0, (strlen($line) - strlen(strrchr($line, '.'))));
  1490. $extension = substr(strrchr($line, '.'), 1);
  1491. // Make sure it is an image.
  1492. if (strcasecmp($extension, 'gif') != 0 && strcasecmp($extension, 'jpg') != 0 && strcasecmp($extension, 'jpeg') != 0 && strcasecmp($extension, 'png') != 0 && strcasecmp($extension, 'bmp') != 0)
  1493. continue;
  1494. $result[] = array(
  1495. 'filename' => htmlspecialchars($line),
  1496. 'checked' => $line == $context['member']['avatar']['server_pic'],
  1497. 'name' => htmlspecialchars(str_replace('_', ' ', $filename)),
  1498. 'is_dir' => false
  1499. );
  1500. if ($level == 1)
  1501. $context['avatar_list'][] = $directory . '/' . $line;
  1502. }
  1503. return $result;
  1504. }
  1505. /**
  1506. * @todo needs a description
  1507. *
  1508. * @param int $memID id_member
  1509. */
  1510. function theme($memID)
  1511. {
  1512. global $txt, $context, $user_profile, $modSettings, $settings, $user_info, $smcFunc;
  1513. loadThemeOptions($memID);
  1514. if (allowedTo(array('profile_extra_own', 'profile_extra_any')))
  1515. loadCustomFields($memID, 'theme');
  1516. $context['sub_template'] = 'edit_options';
  1517. $context['page_desc'] = $txt['theme_info'];
  1518. setupProfileContext(
  1519. array(
  1520. 'id_theme', 'smiley_set', 'hr',
  1521. 'time_format', 'time_offset', 'hr',
  1522. 'theme_settings',
  1523. )
  1524. );
  1525. }
  1526. /**
  1527. * Changing authentication method? Only appropriate for people using OpenID.
  1528. *
  1529. * @param int $memID id_member
  1530. * @param bool $saving = false
  1531. */
  1532. function authentication($memID, $saving = false)
  1533. {
  1534. global $context, $cur_profile, $sourcedir, $txt, $post_errors, $modSettings;
  1535. loadLanguage('Login');
  1536. // We are saving?
  1537. if ($saving)
  1538. {
  1539. // Moving to password passed authentication?
  1540. if ($_POST['authenticate'] == 'passwd')
  1541. {
  1542. // Didn't enter anything?
  1543. if ($_POST['passwrd1'] == '')
  1544. $post_errors[] = 'no_password';
  1545. // Do the two entries for the password even match?
  1546. elseif (!isset($_POST['passwrd2']) || $_POST['passwrd1'] != $_POST['passwrd2'])
  1547. $post_errors[] = 'bad_new_password';
  1548. // Is it valid?
  1549. else
  1550. {
  1551. require_once($sourcedir . '/Subs-Auth.php');
  1552. $passwordErrors = validatePassword($_POST['passwrd1'], $cur_profile['member_name'], array($cur_profile['real_name'], $cur_profile['email_address']));
  1553. // Were there errors?
  1554. if ($passwordErrors != null)
  1555. $post_errors[] = 'password_' . $passwordErrors;
  1556. }
  1557. if (empty($post_errors))
  1558. {
  1559. // Integration?
  1560. call_integration_hook('integrate_reset_pass', array($cur_profile['member_name'], $cur_profile['member_name'], $_POST['passwrd1']));
  1561. // Go then.
  1562. $passwd = sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd1']));
  1563. // Do the important bits.
  1564. updateMemberData($memID, array('openid_uri' => '', 'passwd' => $passwd));
  1565. if ($context['user']['is_owner'])
  1566. setLoginCookie(60 * $modSettings['cookieTime'], $memID, sha1(sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd2'])) . $cur_profile['password_salt']));
  1567. redirectexit('action=profile;u=' . $memID);
  1568. }
  1569. return true;
  1570. }
  1571. // Not right yet!
  1572. elseif ($_POST['authenticate'] == 'openid' && !empty($_POST['openid_identifier']))
  1573. {
  1574. require_once($sourcedir . '/Subs-OpenID.php');
  1575. $_POST['openid_identifier'] = smf_openID_canonize($_POST['openid_identifier']);
  1576. if (smf_openid_member_exists($_POST['openid_identifier']))
  1577. $post_errors[] = 'openid_in_use';
  1578. elseif (empty($post_errors))
  1579. {
  1580. // Authenticate using the new OpenID URI first to make sure they didn't make a mistake.
  1581. if ($context['user']['is_owner'])
  1582. {
  1583. $_SESSION['new_openid_uri'] = $_POST['openid_identifier'];
  1584. smf_openID_validate($_POST['openid_identifier'], false, null, 'change_uri');
  1585. }
  1586. else
  1587. updateMemberData($memID, array('openid_uri' => $_POST['openid_identifier']));
  1588. }
  1589. }
  1590. }
  1591. // Some stuff.
  1592. $context['member']['openid_uri'] = $cur_profile['openid_uri'];
  1593. $context['auth_method'] = empty($cur_profile['openid_uri']) ? 'password' : 'openid';
  1594. $context['sub_template'] = 'authentication_method';
  1595. }
  1596. /**
  1597. * Display the notifications and settings for changes.
  1598. *
  1599. * @param int $memID id_member
  1600. */
  1601. function notification($memID)
  1602. {
  1603. global $txt, $scripturl, $user_profile, $user_info, $context, $modSettings, $smcFunc, $sourcedir, $settings;
  1604. // Gonna want this for the list.
  1605. require_once($sourcedir . '/Subs-List.php');
  1606. // Fine, start with the board list.
  1607. $listOptions = array(
  1608. 'id' => 'board_notification_list',
  1609. 'width' => '100%',
  1610. 'no_items_label' => $txt['notifications_boards_none'] . '<br /><br />' . $txt['notifications_boards_howto'],
  1611. 'no_items_align' => 'left',
  1612. 'base_href' => $scripturl . '?action=profile;u=' . $memID . ';area=notification',
  1613. 'default_sort_col' => 'board_name',
  1614. 'get_items' => array(
  1615. 'function' => 'list_getBoardNotifications',
  1616. 'params' => array(
  1617. $memID,
  1618. ),
  1619. ),
  1620. 'columns' => array(
  1621. 'board_name' => array(
  1622. 'header' => array(
  1623. 'value' => $txt['notifications_boards'],
  1624. 'class' => 'lefttext first_th',
  1625. ),
  1626. 'data' => array(
  1627. 'function' => create_function('$board', '
  1628. global $settings, $txt;
  1629. $link = $board[\'link\'];
  1630. if ($board[\'new\'])
  1631. $link .= \' <a href="\' . $board[\'href\'] . \'"><span class="new_posts">' . $txt['new'] . '</span></a>\';
  1632. return $link;
  1633. '),
  1634. ),
  1635. 'sort' => array(
  1636. 'default' => 'name',
  1637. 'reverse' => 'name DESC',
  1638. ),
  1639. ),
  1640. 'delete' => array(
  1641. 'header' => array(
  1642. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);" />',
  1643. 'style' => 'width: 4%;',
  1644. ),
  1645. 'data' => array(
  1646. 'sprintf' => array(
  1647. 'format' => '<input type="checkbox" name="notify_boards[]" value="%1$d" class="input_check" />',
  1648. 'params' => array(
  1649. 'id' => false,
  1650. ),
  1651. ),
  1652. 'style' => 'text-align: center;',
  1653. ),
  1654. ),
  1655. ),
  1656. 'form' => array(
  1657. 'href' => $scripturl . '?action=profile;area=notification;save',
  1658. 'include_sort' => true,
  1659. 'include_start' => true,
  1660. 'hidden_fields' => array(
  1661. 'u' => $memID,
  1662. 'sa' => $context['menu_item_selected'],
  1663. $context['session_var'] => $context['session_id'],
  1664. ),
  1665. 'token' => $context['token_check'],
  1666. ),
  1667. 'additional_rows' => array(
  1668. array(
  1669. 'position' => 'bottom_of_list',
  1670. 'value' => '<input type="submit" name="edit_notify_boards" value="' . $txt['notifications_update'] . '" class="button_submit" />',
  1671. 'align' => 'right',
  1672. ),
  1673. ),
  1674. );
  1675. // Create the board notification list.
  1676. createList($listOptions);
  1677. // Now do the topic notifications.
  1678. $listOptions = array(
  1679. 'id' => 'topic_notification_list',
  1680. 'width' => '100%',
  1681. 'items_per_page' => $modSettings['defaultMaxMessages'],
  1682. 'no_items_label' => $txt['notifications_topics_none'] . '<br /><br />' . $txt['notifications_topics_howto'],
  1683. 'no_items_align' => 'left',
  1684. 'base_href' => $scripturl . '?action=profile;u=' . $memID . ';area=notification',
  1685. 'default_sort_col' => 'last_post',
  1686. 'get_items' => array(
  1687. 'function' => 'list_getTopicNotifications',
  1688. 'params' => array(
  1689. $memID,
  1690. ),
  1691. ),
  1692. 'get_count' => array(
  1693. 'function' => 'list_getTopicNotificationCount',
  1694. 'params' => array(
  1695. $memID,
  1696. ),
  1697. ),
  1698. 'columns' => array(
  1699. 'subject' => array(
  1700. 'header' => array(
  1701. 'value' => $txt['notifications_topics'],
  1702. 'class' => 'lefttext first_th',
  1703. ),
  1704. 'data' => array(
  1705. 'function' => create_function('$topic', '
  1706. global $settings, $txt;
  1707. $link = $topic[\'link\'];
  1708. if ($topic[\'new\'])
  1709. $link .= \' <a href="\' . $topic[\'new_href\'] . \'"><span class="new_posts">\' . $txt[\'new\'] . \'</span></a>\';
  1710. $link .= \'<br /><span class="smalltext"><em>\' . $txt[\'in\'] . \' \' . $topic[\'board_link\'] . \'</em></span>\';
  1711. return $link;
  1712. '),
  1713. ),
  1714. 'sort' => array(
  1715. 'default' => 'ms.subject',
  1716. 'reverse' => 'ms.subject DESC',
  1717. ),
  1718. ),
  1719. 'started_by' => array(
  1720. 'header' => array(
  1721. 'value' => $txt['started_by'],
  1722. 'class' => 'lefttext',
  1723. ),
  1724. 'data' => array(
  1725. 'db' => 'poster_link',
  1726. ),
  1727. 'sort' => array(
  1728. 'default' => 'real_name_col',
  1729. 'reverse' => 'real_name_col DESC',
  1730. ),
  1731. ),
  1732. 'last_post' => array(
  1733. 'header' => array(
  1734. 'value' => $txt['last_post'],
  1735. 'class' => 'lefttext',
  1736. ),
  1737. 'data' => array(
  1738. 'sprintf' => array(
  1739. 'format' => '<span class="smalltext">%1$s<br />' . $txt['by'] . ' %2$s</span>',
  1740. 'params' => array(
  1741. 'updated' => false,
  1742. 'poster_updated_link' => false,
  1743. ),
  1744. ),
  1745. ),
  1746. 'sort' => array(
  1747. 'default' => 'ml.id_msg DESC',
  1748. 'reverse' => 'ml.id_msg',
  1749. ),
  1750. ),
  1751. 'delete' => array(
  1752. 'header' => array(
  1753. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);" />',
  1754. 'style' => 'width: 4%;',
  1755. ),
  1756. 'data' => array(
  1757. 'sprintf' => array(
  1758. 'format' => '<input type="checkbox" name="notify_topics[]" value="%1$d" class="input_check" />',
  1759. 'params' => array(
  1760. 'id' => false,
  1761. ),
  1762. ),
  1763. 'style' => 'text-align: center;',
  1764. ),
  1765. ),
  1766. ),
  1767. 'form' => array(
  1768. 'href' => $scripturl . '?action=profile;area=notification;save',
  1769. 'include_sort' => true,
  1770. 'include_start' => true,
  1771. 'hidden_fields' => array(
  1772. 'u' => $memID,
  1773. 'sa' => $context['menu_item_selected'],
  1774. $context['session_var'] => $context['session_id'],
  1775. ),
  1776. 'token' => $context['token_check'],
  1777. ),
  1778. 'additional_rows' => array(
  1779. array(
  1780. 'position' => 'bottom_of_list',
  1781. 'value' => '<input type="submit" name="edit_notify_topics" value="' . $txt['notifications_update'] . '" class="button_submit" />',
  1782. 'align' => 'right',
  1783. ),
  1784. ),
  1785. );
  1786. // Create the notification list.
  1787. createList($listOptions);
  1788. // What options are set?
  1789. $context['member'] += array(
  1790. 'notify_announcements' => $user_profile[$memID]['notify_announcements'],
  1791. 'notify_send_body' => $user_profile[$memID]['notify_send_body'],
  1792. 'notify_types' => $user_profile[$memID]['notify_types'],
  1793. 'notify_regularity' => $user_profile[$memID]['notify_regularity'],
  1794. );
  1795. loadThemeOptions($memID);
  1796. }
  1797. /**
  1798. * @todo needs a description
  1799. *
  1800. * @param int $memID id_member
  1801. * @return string
  1802. */
  1803. function list_getTopicNotificationCount($memID)
  1804. {
  1805. global $smcFunc, $user_info, $context, $modSettings;
  1806. $request = $smcFunc['db_query']('', '
  1807. SELECT COUNT(*)
  1808. FROM {db_prefix}log_notify AS ln' . (!$modSettings['postmod_active'] && $user_info['query_see_board'] === '1=1' ? '' : '
  1809. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic)') . ($user_info['query_see_board'] === '1=1' ? '' : '
  1810. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)') . '
  1811. WHERE ln.id_member = {int:selected_member}' . ($user_info['query_see_board'] === '1=1' ? '' : '
  1812. AND {query_see_board}') . ($modSettings['postmod_active'] ? '
  1813. AND t.approved = {int:is_approved}' : ''),
  1814. array(
  1815. 'selected_member' => $memID,
  1816. 'is_approved' => 1,
  1817. )
  1818. );
  1819. list ($totalNotifications) = $smcFunc['db_fetch_row']($request);
  1820. $smcFunc['db_free_result']($request);
  1821. // @todo make this an integer before it gets returned
  1822. return $totalNotifications;
  1823. }
  1824. /**
  1825. * @todo Needs a description
  1826. *
  1827. * @param int $start
  1828. * @param int $items_per_page
  1829. * @param string $sort
  1830. * @param int $memID id_member
  1831. * @return array $notification_topics
  1832. */
  1833. function list_getTopicNotifications($start, $items_per_page, $sort, $memID)
  1834. {
  1835. global $smcFunc, $txt, $scripturl, $user_info, $context, $modSettings;
  1836. // All the topics with notification on...
  1837. $request = $smcFunc['db_query']('', '
  1838. SELECT
  1839. IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from, b.id_board, b.name,
  1840. t.id_topic, ms.subject, ms.id_member, IFNULL(mem.real_name, ms.poster_name) AS real_name_col,
  1841. ml.id_msg_modified, ml.poster_time, ml.id_member AS id_member_updated,
  1842. IFNULL(mem2.real_name, ml.poster_name) AS last_real_name
  1843. FROM {db_prefix}log_notify AS ln
  1844. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ')
  1845. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board AND {query_see_board})
  1846. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
  1847. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  1848. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ms.id_member)
  1849. LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = ml.id_member)
  1850. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  1851. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})
  1852. WHERE ln.id_member = {int:selected_member}
  1853. ORDER BY {raw:sort}
  1854. LIMIT {int:offset}, {int:items_per_page}',
  1855. array(
  1856. 'current_member' => $user_info['id'],
  1857. 'is_approved' => 1,
  1858. 'selected_member' => $memID,
  1859. 'sort' => $sort,
  1860. 'offset' => $start,
  1861. 'items_per_page' => $items_per_page,
  1862. )
  1863. );
  1864. $notification_topics = array();
  1865. while ($row = $smcFunc['db_fetch_assoc']($request))
  1866. {
  1867. censorText($row['subject']);
  1868. $notification_topics[] = array(
  1869. 'id' => $row['id_topic'],
  1870. 'poster_link' => empty($row['id_member']) ? $row['real_name_col'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name_col'] . '</a>',
  1871. 'poster_updated_link' => empty($row['id_member_updated']) ? $row['last_real_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_updated'] . '">' . $row['last_real_name'] . '</a>',
  1872. 'subject' => $row['subject'],
  1873. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  1874. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
  1875. 'new' => $row['new_from'] <= $row['id_msg_modified'],
  1876. 'new_from' => $row['new_from'],
  1877. 'updated' => timeformat($row['poster_time']),
  1878. 'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new',
  1879. 'new_link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new">' . $row['subject'] . '</a>',
  1880. 'board_link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
  1881. );
  1882. }
  1883. $smcFunc['db_free_result']($request);
  1884. return $notification_topics;
  1885. }
  1886. /**
  1887. * @todo needs a description
  1888. *
  1889. * @param int $start
  1890. * @param int $items_per_page
  1891. * @param string $sort
  1892. * @param int $memID id_member
  1893. * @return array
  1894. */
  1895. function list_getBoardNotifications($start, $items_per_page, $sort, $memID)
  1896. {
  1897. global $smcFunc, $txt, $scripturl, $user_info;
  1898. $request = $smcFunc['db_query']('', '
  1899. SELECT b.id_board, b.name, IFNULL(lb.id_msg, 0) AS board_read, b.id_msg_updated
  1900. FROM {db_prefix}log_notify AS ln
  1901. INNER JOIN {db_prefix}boards AS b ON (b.id_board = ln.id_board)
  1902. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
  1903. WHERE ln.id_member = {int:selected_member}
  1904. AND {query_see_board}
  1905. ORDER BY ' . $sort,
  1906. array(
  1907. 'current_member' => $user_info['id'],
  1908. 'selected_member' => $memID,
  1909. )
  1910. );
  1911. $notification_boards = array();
  1912. while ($row = $smcFunc['db_fetch_assoc']($request))
  1913. $notification_boards[] = array(
  1914. 'id' => $row['id_board'],
  1915. 'name' => $row['name'],
  1916. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  1917. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
  1918. 'new' => $row['board_read'] < $row['id_msg_updated']
  1919. );
  1920. $smcFunc['db_free_result']($request);
  1921. return $notification_boards;
  1922. }
  1923. /**
  1924. * @todo needs a description
  1925. *
  1926. * @param int $memID id_member
  1927. */
  1928. function loadThemeOptions($memID)
  1929. {
  1930. global $context, $options, $cur_profile, $smcFunc;
  1931. if (isset($_POST['default_options']))
  1932. $_POST['options'] = isset($_POST['options']) ? $_POST['options'] + $_POST['default_options'] : $_POST['default_options'];
  1933. if ($context['user']['is_owner'])
  1934. {
  1935. $context['member']['options'] = $options;
  1936. if (isset($_POST['options']) && is_array($_POST['options']))
  1937. foreach ($_POST['options'] as $k => $v)
  1938. $context['member']['options'][$k] = $v;
  1939. }
  1940. else
  1941. {
  1942. $request = $smcFunc['db_query']('', '
  1943. SELECT id_member, variable, value
  1944. FROM {db_prefix}themes
  1945. WHERE id_theme IN (1, {int:member_theme})
  1946. AND id_member IN (-1, {int:selected_member})',
  1947. array(
  1948. 'member_theme' => (int) $cur_profile['id_theme'],
  1949. 'selected_member' => $memID,
  1950. )
  1951. );
  1952. $temp = array();
  1953. while ($row = $smcFunc['db_fetch_assoc']($request))
  1954. {
  1955. if ($row['id_member'] == -1)
  1956. {
  1957. $temp[$row['variable']] = $row['value'];
  1958. continue;
  1959. }
  1960. if (isset($_POST['options'][$row['variable']]))
  1961. $row['value'] = $_POST['options'][$row['variable']];
  1962. $context['member']['options'][$row['variable']] = $row['value'];
  1963. }
  1964. $smcFunc['db_free_result']($request);
  1965. // Load up the default theme options for any missing.
  1966. foreach ($temp as $k => $v)
  1967. {
  1968. if (!isset($context['member']['options'][$k]))
  1969. $context['member']['options'][$k] = $v;
  1970. }
  1971. }
  1972. }
  1973. /**
  1974. * @todo needs a description
  1975. *
  1976. * @param int $memID id_member
  1977. */
  1978. function ignoreboards($memID)
  1979. {
  1980. global $txt, $user_info, $context, $modSettings, $smcFunc, $cur_profile;
  1981. // Have the admins enabled this option?
  1982. if (empty($modSettings['allow_ignore_boards']))
  1983. fatal_lang_error('ignoreboards_disallowed', 'user');
  1984. // Find all the boards this user is allowed to see.
  1985. $request = $smcFunc['db_query']('order_by_board_order', '
  1986. SELECT b.id_cat, c.name AS cat_name, b.id_board, b.name, b.child_level,
  1987. '. (!empty($cur_profile['ignore_boards']) ? 'b.id_board IN ({array_int:ignore_boards})' : '0') . ' AS is_ignored
  1988. FROM {db_prefix}boards AS b
  1989. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  1990. WHERE {query_see_board}
  1991. AND redirect = {string:empty_string}',
  1992. array(
  1993. 'ignore_boards' => !empty($cur_profile['ignore_boards']) ? explode(',', $cur_profile['ignore_boards']) : array(),
  1994. 'empty_string' => '',
  1995. )
  1996. );
  1997. $context['num_boards'] = $smcFunc['db_num_rows']($request);
  1998. $context['categories'] = array();
  1999. while ($row = $smcFunc['db_fetch_assoc']($request))
  2000. {
  2001. // This category hasn't been set up yet..
  2002. if (!isset($context['categories'][$row['id_cat']]))
  2003. $context['categories'][$row['id_cat']] = array(
  2004. 'id' => $row['id_cat'],
  2005. 'name' => $row['cat_name'],
  2006. 'boards' => array()
  2007. );
  2008. // Set this board up, and let the template know when it's a child. (indent them..)
  2009. $context['categories'][$row['id_cat']]['boards'][$row['id_board']] = array(
  2010. 'id' => $row['id_board'],
  2011. 'name' => $row['name'],
  2012. 'child_level' => $row['child_level'],
  2013. 'selected' => $row['is_ignored'],
  2014. );
  2015. }
  2016. $smcFunc['db_free_result']($request);
  2017. // Now, let's sort the list of categories into the boards for templates that like that.
  2018. $temp_boards = array();
  2019. foreach ($context['categories'] as $category)
  2020. {
  2021. // Include a list of boards per category for easy toggling.
  2022. $context['categories'][$category['id']]['child_ids'] = array_keys($category['boards']);
  2023. $temp_boards[] = array(
  2024. 'name' => $category['name'],
  2025. 'child_ids' => array_keys($category['boards'])
  2026. );
  2027. $temp_boards = array_merge($temp_boards, array_values($category['boards']));
  2028. }
  2029. $max_boards = ceil(count($temp_boards) / 2);
  2030. if ($max_boards == 1)
  2031. $max_boards = 2;
  2032. // Now, alternate them so they can be shown left and right ;).
  2033. $context['board_columns'] = array();
  2034. for ($i = 0; $i < $max_boards; $i++)
  2035. {
  2036. $context['board_columns'][] = $temp_boards[$i];
  2037. if (isset($temp_boards[$i + $max_boards]))
  2038. $context['board_columns'][] = $temp_boards[$i + $max_boards];
  2039. else
  2040. $context['board_columns'][] = array();
  2041. }
  2042. loadThemeOptions($memID);
  2043. }
  2044. /**
  2045. * Load all the languages for the profile.
  2046. * @return bool
  2047. */
  2048. function profileLoadLanguages()
  2049. {
  2050. global $context, $modSettings, $settings, $cur_profile, $language, $smcFunc;
  2051. $context['profile_languages'] = array();
  2052. // Get our languages!
  2053. getLanguages(true, true);
  2054. // Setup our languages.
  2055. foreach ($context['languages'] as $lang)
  2056. {
  2057. $context['profile_languages'][$lang['filename']] = strtr($lang['name'], array('-utf8' => ''));
  2058. }
  2059. ksort($context['profile_languages']);
  2060. // Return whether we should proceed with this.
  2061. return count($context['profile_languages']) > 1 ? true : false;
  2062. }
  2063. /**
  2064. * @todo needs description
  2065. *
  2066. * @return true
  2067. */
  2068. function profileLoadGroups()
  2069. {
  2070. global $cur_profile, $txt, $context, $smcFunc, $user_settings;
  2071. $context['member_groups'] = array(
  2072. 0 => array(
  2073. 'id' => 0,
  2074. 'name' => $txt['no_primary_membergroup'],
  2075. 'is_primary' => $cur_profile['id_group'] == 0,
  2076. 'can_be_additional' => false,
  2077. 'can_be_primary' => true,
  2078. )
  2079. );
  2080. $curGroups = explode(',', $cur_profile['additional_groups']);
  2081. // Load membergroups, but only those groups the user can assign.
  2082. $request = $smcFunc['db_query']('', '
  2083. SELECT group_name, id_group, hidden
  2084. FROM {db_prefix}membergroups
  2085. WHERE id_group != {int:moderator_group}
  2086. AND min_posts = {int:min_posts}' . (allowedTo('admin_forum') ? '' : '
  2087. AND group_type != {int:is_protected}') . '
  2088. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  2089. array(
  2090. 'moderator_group' => 3,
  2091. 'min_posts' => -1,
  2092. 'is_protected' => 1,
  2093. 'newbie_group' => 4,
  2094. )
  2095. );
  2096. while ($row = $smcFunc['db_fetch_assoc']($request))
  2097. {
  2098. // We should skip the administrator group if they don't have the admin_forum permission!
  2099. if ($row['id_group'] == 1 && !allowedTo('admin_forum'))
  2100. continue;
  2101. $context['member_groups'][$row['id_group']] = array(
  2102. 'id' => $row['id_group'],
  2103. 'name' => $row['group_name'],
  2104. 'is_primary' => $cur_profile['id_group'] == $row['id_group'],
  2105. 'is_additional' => in_array($row['id_group'], $curGroups),
  2106. 'can_be_additional' => true,
  2107. 'can_be_primary' => $row['hidden'] != 2,
  2108. );
  2109. }
  2110. $smcFunc['db_free_result']($request);
  2111. $context['member']['group_id'] = $user_settings['id_group'];
  2112. return true;
  2113. }
  2114. /**
  2115. * Load key signature context data.
  2116. *
  2117. * @return true
  2118. */
  2119. function profileLoadSignatureData()
  2120. {
  2121. global $modSettings, $context, $txt, $cur_profile, $smcFunc;
  2122. // Signature limits.
  2123. list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
  2124. $sig_limits = explode(',', $sig_limits);
  2125. $context['signature_enabled'] = isset($sig_limits[0]) ? $sig_limits[0] : 0;
  2126. $context['signature_limits'] = array(
  2127. 'max_length' => isset($sig_limits[1]) ? $sig_limits[1] : 0,
  2128. 'max_lines' => isset($sig_limits[2]) ? $sig_limits[2] : 0,
  2129. 'max_images' => isset($sig_limits[3]) ? $sig_limits[3] : 0,
  2130. 'max_smileys' => isset($sig_limits[4]) ? $sig_limits[4] : 0,
  2131. 'max_image_width' => isset($sig_limits[5]) ? $sig_limits[5] : 0,
  2132. 'max_image_height' => isset($sig_limits[6]) ? $sig_limits[6] : 0,
  2133. 'max_font_size' => isset($sig_limits[7]) ? $sig_limits[7] : 0,
  2134. 'bbc' => !empty($sig_bbc) ? explode(',', $sig_bbc) : array(),
  2135. );
  2136. // Kept this line in for backwards compatibility!
  2137. $context['max_signature_length'] = $context['signature_limits']['max_length'];
  2138. // Warning message for signature image limits?
  2139. $context['signature_warning'] = '';
  2140. if ($context['signature_limits']['max_image_width'] && $context['signature_limits']['max_image_height'])
  2141. $context['signature_warning'] = sprintf($txt['profile_error_signature_max_image_size'], $context['signature_limits']['max_image_width'], $context['signature_limits']['max_image_height']);
  2142. elseif ($context['signature_limits']['max_image_width'] || $context['signature_limits']['max_image_height'])
  2143. $context['signature_warning'] = sprintf($txt['profile_error_signature_max_image_' . ($context['signature_limits']['max_image_width'] ? 'width' : 'height')], $context['signature_limits'][$context['signature_limits']['max_image_width'] ? 'max_image_width' : 'max_image_height']);
  2144. $context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && function_exists('pspell_new');
  2145. $context['member']['signature'] = empty($cur_profile['signature']) ? '' : str_replace(array('<br />', '<', '>', '"', '\''), array("\n", '&lt;', '&gt;', '&quot;', '&#039;'), $cur_profile['signature']);
  2146. return true;
  2147. }
  2148. /**
  2149. * Load avatar context data.
  2150. *
  2151. * @return true
  2152. */
  2153. function profileLoadAvatarData()
  2154. {
  2155. global $context, $cur_profile, $modSettings, $scripturl;
  2156. $context['avatar_url'] = $modSettings['avatar_url'];
  2157. // Default context.
  2158. $context['member']['avatar'] += array(
  2159. 'custom' => stristr($cur_profile['avatar'], 'http://') ? $cur_profile['avatar'] : 'http://',
  2160. 'selection' => $cur_profile['avatar'] == '' || stristr($cur_profile['avatar'], 'http://') ? '' : $cur_profile['avatar'],
  2161. 'id_attach' => $cur_profile['id_attach'],
  2162. 'filename' => $cur_profile['filename'],
  2163. 'allow_server_stored' => allowedTo('profile_server_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2164. 'allow_upload' => allowedTo('profile_upload_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2165. 'allow_external' => allowedTo('profile_remote_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2166. );
  2167. if ($cur_profile['avatar'] == '' && $cur_profile['id_attach'] > 0 && $context['member']['avatar']['allow_upload'])
  2168. {
  2169. $context['member']['avatar'] += array(
  2170. 'choice' => 'upload',
  2171. 'server_pic' => 'blank.png',
  2172. 'external' => 'http://'
  2173. );
  2174. $context['member']['avatar']['href'] = empty($cur_profile['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $cur_profile['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $cur_profile['filename'];
  2175. }
  2176. elseif (stristr($cur_profile['avatar'], 'http://') && $context['member']['avatar']['allow_external'])
  2177. $context['member']['avatar'] += array(
  2178. 'choice' => 'external',
  2179. 'server_pic' => 'blank.png',
  2180. 'external' => $cur_profile['avatar']
  2181. );
  2182. elseif ($cur_profile['avatar'] != '' && file_exists($modSettings['avatar_directory'] . '/' . $cur_profile['avatar']) && $context['member']['avatar']['allow_server_stored'])
  2183. $context['member']['avatar'] += array(
  2184. 'choice' => 'server_stored',
  2185. 'server_pic' => $cur_profile['avatar'] == '' ? 'blank.png' : $cur_profile['avatar'],
  2186. 'external' => 'http://'
  2187. );
  2188. else
  2189. $context['member']['avatar'] += array(
  2190. 'choice' => 'none',
  2191. 'server_pic' => 'blank.png',
  2192. 'external' => 'http://'
  2193. );
  2194. // Get a list of all the avatars.
  2195. if ($context['member']['avatar']['allow_server_stored'])
  2196. {
  2197. $context['avatar_list'] = array();
  2198. $context['avatars'] = is_dir($modSettings['avatar_directory']) ? getAvatars('', 0) : array();
  2199. }
  2200. else
  2201. $context['avatars'] = array();
  2202. // Second level selected avatar...
  2203. $context['avatar_selected'] = substr(strrchr($context['member']['avatar']['server_pic'], '/'), 1);
  2204. return true;
  2205. }
  2206. /**
  2207. * Save a members group.
  2208. *
  2209. * @param int &$value
  2210. * @return true
  2211. */
  2212. function profileSaveGroups(&$value)
  2213. {
  2214. global $profile_vars, $old_profile, $context, $smcFunc, $cur_profile;
  2215. // Do we need to protect some groups?
  2216. if (!allowedTo('admin_forum'))
  2217. {
  2218. $request = $smcFunc['db_query']('', '
  2219. SELECT id_group
  2220. FROM {db_prefix}membergroups
  2221. WHERE group_type = {int:is_protected}',
  2222. array(
  2223. 'is_protected' => 1,
  2224. )
  2225. );
  2226. $protected_groups = array(1);
  2227. while ($row = $smcFunc['db_fetch_assoc']($request))
  2228. $protected_groups[] = $row['id_group'];
  2229. $smcFunc['db_free_result']($request);
  2230. $protected_groups = array_unique($protected_groups);
  2231. }
  2232. // The account page allows the change of your id_group - but not to a protected group!
  2233. if (empty($protected_groups) || count(array_intersect(array((int) $value, $old_profile['id_group']), $protected_groups)) == 0)
  2234. $value = (int) $value;
  2235. // ... otherwise it's the old group sir.
  2236. else
  2237. $value = $old_profile['id_group'];
  2238. // Find the additional membergroups (if any)
  2239. if (isset($_POST['additional_groups']) && is_array($_POST['additional_groups']))
  2240. {
  2241. $additional_groups = array();
  2242. foreach ($_POST['additional_groups'] as $group_id)
  2243. {
  2244. $group_id = (int) $group_id;
  2245. if (!empty($group_id) && (empty($protected_groups) || !in_array($group_id, $protected_groups)))
  2246. $additional_groups[] = $group_id;
  2247. }
  2248. // Put the protected groups back in there if you don't have permission to take them away.
  2249. $old_additional_groups = explode(',', $old_profile['additional_groups']);
  2250. foreach ($old_additional_groups as $group_id)
  2251. {
  2252. if (!empty($protected_groups) && in_array($group_id, $protected_groups))
  2253. $additional_groups[] = $group_id;
  2254. }
  2255. if (implode(',', $additional_groups) !== $old_profile['additional_groups'])
  2256. {
  2257. $profile_vars['additional_groups'] = implode(',', $additional_groups);
  2258. $cur_profile['additional_groups'] = implode(',', $additional_groups);
  2259. }
  2260. }
  2261. // Too often, people remove delete their own account, or something.
  2262. if (in_array(1, explode(',', $old_profile['additional_groups'])) || $old_profile['id_group'] == 1)
  2263. {
  2264. $stillAdmin = $value == 1 || (isset($additional_groups) && in_array(1, $additional_groups));
  2265. // If they would no longer be an admin, look for any other...
  2266. if (!$stillAdmin)
  2267. {
  2268. $request = $smcFunc['db_query']('', '
  2269. SELECT id_member
  2270. FROM {db_prefix}members
  2271. WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
  2272. AND id_member != {int:selected_member}
  2273. LIMIT 1',
  2274. array(
  2275. 'admin_group' => 1,
  2276. 'selected_member' => $context['id_member'],
  2277. )
  2278. );
  2279. list ($another) = $smcFunc['db_fetch_row']($request);
  2280. $smcFunc['db_free_result']($request);
  2281. if (empty($another))
  2282. fatal_lang_error('at_least_one_admin', 'critical');
  2283. }
  2284. }
  2285. // If we are changing group status, update permission cache as necessary.
  2286. if ($value != $old_profile['id_group'] || isset($profile_vars['additional_groups']))
  2287. {
  2288. if ($context['user']['is_owner'])
  2289. $_SESSION['mc']['time'] = 0;
  2290. else
  2291. updateSettings(array('settings_updated' => time()));
  2292. }
  2293. return true;
  2294. }
  2295. /**
  2296. * The avatar is incredibly complicated, what with the options... and what not.
  2297. * @todo argh, the avatar here. Take this out of here!
  2298. *
  2299. * @param array &$value
  2300. * @return mixed
  2301. */
  2302. function profileSaveAvatarData(&$value)
  2303. {
  2304. global $modSettings, $sourcedir, $smcFunc, $profile_vars, $cur_profile, $context;
  2305. $memID = $context['id_member'];
  2306. if (empty($memID) && !empty($context['password_auth_failed']))
  2307. return false;
  2308. require_once($sourcedir . '/ManageAttachments.php');
  2309. // We need to know where we're going to be putting it..
  2310. if (!empty($modSettings['custom_avatar_enabled']))
  2311. {
  2312. $uploadDir = $modSettings['custom_avatar_dir'];
  2313. $id_folder = 1;
  2314. }
  2315. elseif (!empty($modSettings['currentAttachmentUploadDir']))
  2316. {
  2317. if (!is_array($modSettings['attachmentUploadDir']))
  2318. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
  2319. // Just use the current path for temp files.
  2320. $uploadDir = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']];
  2321. $id_folder = $modSettings['currentAttachmentUploadDir'];
  2322. }
  2323. else
  2324. {
  2325. $uploadDir = $modSettings['attachmentUploadDir'];
  2326. $id_folder = 1;
  2327. }
  2328. $downloadedExternalAvatar = false;
  2329. if ($value == 'external' && allowedTo('profile_remote_avatar') && stripos($_POST['userpicpersonal'], 'http://') === 0 && strlen($_POST['userpicpersonal']) > 7 && !empty($modSettings['avatar_download_external']))
  2330. {
  2331. if (!is_writable($uploadDir))
  2332. fatal_lang_error('attachments_no_write', 'critical');
  2333. require_once($sourcedir . '/Subs-Package.php');
  2334. $url = parse_url($_POST['userpicpersonal']);
  2335. $contents = fetch_web_data('http://' . $url['host'] . (empty($url['port']) ? '' : ':' . $url['port']) . str_replace(' ', '%20', trim($url['path'])));
  2336. if ($contents != false && $tmpAvatar = fopen($uploadDir . '/avatar_tmp_' . $memID, 'wb'))
  2337. {
  2338. fwrite($tmpAvatar, $contents);
  2339. fclose($tmpAvatar);
  2340. $downloadedExternalAvatar = true;
  2341. $_FILES['attachment']['tmp_name'] = $uploadDir . '/avatar_tmp_' . $memID;
  2342. }
  2343. }
  2344. if ($value == 'none')
  2345. {
  2346. $profile_vars['avatar'] = '';
  2347. // Reset the attach ID.
  2348. $cur_profile['id_attach'] = 0;
  2349. $cur_profile['attachment_type'] = 0;
  2350. $cur_profile['filename'] = '';
  2351. removeAttachments(array('id_member' => $memID));
  2352. }
  2353. elseif ($value == 'server_stored' && allowedTo('profile_server_avatar'))
  2354. {
  2355. $profile_vars['avatar'] = strtr(empty($_POST['file']) ? (empty($_POST['cat']) ? '' : $_POST['cat']) : $_POST['file'], array('&amp;' => '&'));
  2356. $profile_vars['avatar'] = preg_match('~^([\w _!@%*=\-#()\[\]&.,]+/)?[\w _!@%*=\-#()\[\]&.,]+$~', $profile_vars['avatar']) != 0 && preg_match('/\.\./', $profile_vars['avatar']) == 0 && file_exists($modSettings['avatar_directory'] . '/' . $profile_vars['avatar']) ? ($profile_vars['avatar'] == 'blank.png' ? '' : $profile_vars['avatar']) : '';
  2357. // Clear current profile...
  2358. $cur_profile['id_attach'] = 0;
  2359. $cur_profile['attachment_type'] = 0;
  2360. $cur_profile['filename'] = '';
  2361. // Get rid of their old avatar. (if uploaded.)
  2362. removeAttachments(array('id_member' => $memID));
  2363. }
  2364. elseif ($value == 'external' && allowedTo('profile_remote_avatar') && stripos($_POST['userpicpersonal'], 'http://') === 0 && empty($modSettings['avatar_download_external']))
  2365. {
  2366. // We need these clean...
  2367. $cur_profile['id_attach'] = 0;
  2368. $cur_profile['attachment_type'] = 0;
  2369. $cur_profile['filename'] = '';
  2370. // Remove any attached avatar...
  2371. removeAttachments(array('id_member' => $memID));
  2372. // @todo http://www.simplemachines.org/community/index.php?topic=462089.msg3226650#msg3226650
  2373. $profile_vars['avatar'] = str_replace(' ', '%20', preg_replace('~action(?:=|%3d)(?!dlattach)~i', 'action-', $_POST['userpicpersonal']));
  2374. if ($profile_vars['avatar'] == 'http://' || $profile_vars['avatar'] == 'http:///')
  2375. $profile_vars['avatar'] = '';
  2376. // Trying to make us do something we'll regret?
  2377. elseif (substr($profile_vars['avatar'], 0, 7) != 'http://')
  2378. return 'bad_avatar';
  2379. // Should we check dimensions?
  2380. elseif (!empty($modSettings['avatar_max_height_external']) || !empty($modSettings['avatar_max_width_external']))
  2381. {
  2382. // Now let's validate the avatar.
  2383. $sizes = url_image_size($profile_vars['avatar']);
  2384. if (is_array($sizes) && (($sizes[0] > $modSettings['avatar_max_width_external'] && !empty($modSettings['avatar_max_width_external'])) || ($sizes[1] > $modSettings['avatar_max_height_external'] && !empty($modSettings['avatar_max_height_external']))))
  2385. {
  2386. // Houston, we have a problem. The avatar is too large!!
  2387. if ($modSettings['avatar_action_too_large'] == 'option_refuse')
  2388. return 'bad_avatar';
  2389. elseif ($modSettings['avatar_action_too_large'] == 'option_download_and_resize')
  2390. {
  2391. // @todo remove this if appropriate
  2392. require_once($sourcedir . '/Subs-Graphics.php');
  2393. if (downloadAvatar($profile_vars['avatar'], $memID, $modSettings['avatar_max_width_external'], $modSettings['avatar_max_height_external']))
  2394. {
  2395. $profile_vars['avatar'] = '';
  2396. $cur_profile['id_attach'] = $modSettings['new_avatar_data']['id'];
  2397. $cur_profile['filename'] = $modSettings['new_avatar_data']['filename'];
  2398. $cur_profile['attachment_type'] = $modSettings['new_avatar_data']['type'];
  2399. }
  2400. else
  2401. return 'bad_avatar';
  2402. }
  2403. }
  2404. }
  2405. }
  2406. elseif (($value == 'upload' && allowedTo('profile_upload_avatar')) || $downloadedExternalAvatar)
  2407. {
  2408. if ((isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != '') || $downloadedExternalAvatar)
  2409. {
  2410. // Get the dimensions of the image.
  2411. if (!$downloadedExternalAvatar)
  2412. {
  2413. if (!is_writable($uploadDir))
  2414. fatal_lang_error('attachments_no_write', 'critical');
  2415. if (!move_uploaded_file($_FILES['attachment']['tmp_name'], $uploadDir . '/avatar_tmp_' . $memID))
  2416. fatal_lang_error('attach_timeout', 'critical');
  2417. $_FILES['attachment']['tmp_name'] = $uploadDir . '/avatar_tmp_' . $memID;
  2418. }
  2419. $sizes = @getimagesize($_FILES['attachment']['tmp_name']);
  2420. // No size, then it's probably not a valid pic.
  2421. if ($sizes === false)
  2422. return 'bad_avatar';
  2423. // Check whether the image is too large.
  2424. elseif ((!empty($modSettings['avatar_max_width_upload']) && $sizes[0] > $modSettings['avatar_max_width_upload']) || (!empty($modSettings['avatar_max_height_upload']) && $sizes[1] > $modSettings['avatar_max_height_upload']))
  2425. {
  2426. if (!empty($modSettings['avatar_resize_upload']))
  2427. {
  2428. // Attempt to chmod it.
  2429. @chmod($uploadDir . '/avatar_tmp_' . $memID, 0644);
  2430. // @todo remove this require when appropriate
  2431. require_once($sourcedir . '/Subs-Graphics.php');
  2432. if (!downloadAvatar($uploadDir . '/avatar_tmp_' . $memID, $memID, $modSettings['avatar_max_width_upload'], $modSettings['avatar_max_height_upload']))
  2433. return 'bad_avatar';
  2434. // Reset attachment avatar data.
  2435. $cur_profile['id_attach'] = $modSettings['new_avatar_data']['id'];
  2436. $cur_profile['filename'] = $modSettings['new_avatar_data']['filename'];
  2437. $cur_profile['attachment_type'] = $modSettings['new_avatar_data']['type'];
  2438. }
  2439. else
  2440. return 'bad_avatar';
  2441. }
  2442. elseif (is_array($sizes))
  2443. {
  2444. // Now try to find an infection.
  2445. require_once($sourcedir . '/Subs-Graphics.php');
  2446. if (!checkImageContents($_FILES['attachment']['tmp_name'], !empty($modSettings['avatar_paranoid'])))
  2447. {
  2448. // It's bad. Try to re-encode the contents?
  2449. if (empty($modSettings['avatar_reencode']) || (!reencodeImage($_FILES['attachment']['tmp_name'], $sizes[2])))
  2450. return 'bad_avatar';
  2451. // We were successful. However, at what price?
  2452. $sizes = @getimagesize($_FILES['attachment']['tmp_name']);
  2453. // Hard to believe this would happen, but can you bet?
  2454. if ($sizes === false)
  2455. return 'bad_avatar';
  2456. }
  2457. $extensions = array(
  2458. '1' => 'gif',
  2459. '2' => 'jpg',
  2460. '3' => 'png',
  2461. '6' => 'bmp'
  2462. );
  2463. $extension = isset($extensions[$sizes[2]]) ? $extensions[$sizes[2]] : 'bmp';
  2464. $mime_type = 'image/' . ($extension === 'jpg' ? 'jpeg' : ($extension === 'bmp' ? 'x-ms-bmp' : $extension));
  2465. $destName = 'avatar_' . $memID . '_' . time() . '.' . $extension;
  2466. list ($width, $height) = getimagesize($_FILES['attachment']['tmp_name']);
  2467. $file_hash = empty($modSettings['custom_avatar_enabled']) ? getAttachmentFilename($destName, false, null, true) : '';
  2468. // Remove previous attachments this member might have had.
  2469. removeAttachments(array('id_member' => $memID));
  2470. $smcFunc['db_insert']('',
  2471. '{db_prefix}attachments',
  2472. array(
  2473. 'id_member' => 'int', 'attachment_type' => 'int', 'filename' => 'string', 'file_hash' => 'string', 'fileext' => 'string', 'size' => 'int',
  2474. 'width' => 'int', 'height' => 'int', 'mime_type' => 'string', 'id_folder' => 'int',
  2475. ),
  2476. array(
  2477. $memID, (empty($modSettings['custom_avatar_enabled']) ? 0 : 1), $destName, $file_hash, $extension, filesize($_FILES['attachment']['tmp_name']),
  2478. (int) $width, (int) $height, $mime_type, $id_folder,
  2479. ),
  2480. array('id_attach')
  2481. );
  2482. $cur_profile['id_attach'] = $smcFunc['db_insert_id']('{db_prefix}attachments', 'id_attach');
  2483. $cur_profile['filename'] = $destName;
  2484. $cur_profile['attachment_type'] = empty($modSettings['custom_avatar_enabled']) ? 0 : 1;
  2485. $destinationPath = $uploadDir . '/' . (empty($file_hash) ? $destName : $cur_profile['id_attach'] . '_' . $file_hash);
  2486. if (!rename($_FILES['attachment']['tmp_name'], $destinationPath))
  2487. {
  2488. // I guess a man can try.
  2489. removeAttachments(array('id_member' => $memID));
  2490. fatal_lang_error('attach_timeout', 'critical');
  2491. }
  2492. // Attempt to chmod it.
  2493. @chmod($uploadDir . '/' . $destinationPath, 0644);
  2494. }
  2495. $profile_vars['avatar'] = '';
  2496. // Delete any temporary file.
  2497. if (file_exists($uploadDir . '/avatar_tmp_' . $memID))
  2498. @unlink($uploadDir . '/avatar_tmp_' . $memID);
  2499. }
  2500. // Selected the upload avatar option and had one already uploaded before or didn't upload one.
  2501. else
  2502. $profile_vars['avatar'] = '';
  2503. }
  2504. else
  2505. $profile_vars['avatar'] = '';
  2506. // Setup the profile variables so it shows things right on display!
  2507. $cur_profile['avatar'] = $profile_vars['avatar'];
  2508. return false;
  2509. }
  2510. /**
  2511. * Validate the signature
  2512. *
  2513. * @param mixed &$value
  2514. * @return bool|string
  2515. */
  2516. function profileValidateSignature(&$value)
  2517. {
  2518. global $sourcedir, $modSettings, $smcFunc, $txt;
  2519. require_once($sourcedir . '/Subs-Post.php');
  2520. // Admins can do whatever they hell they want!
  2521. if (!allowedTo('admin_forum'))
  2522. {
  2523. // Load all the signature limits.
  2524. list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
  2525. $sig_limits = explode(',', $sig_limits);
  2526. $disabledTags = !empty($sig_bbc) ? explode(',', $sig_bbc) : array();
  2527. $unparsed_signature = strtr(un_htmlspecialchars($value), array("\r" => '', '&#039' => '\''));
  2528. // Too long?
  2529. if (!empty($sig_limits[1]) && $smcFunc['strlen']($unparsed_signature) > $sig_limits[1])
  2530. {
  2531. $_POST['signature'] = trim(htmlspecialchars($smcFunc['substr']($unparsed_signature, 0, $sig_limits[1]), ENT_QUOTES));
  2532. $txt['profile_error_signature_max_length'] = sprintf($txt['profile_error_signature_max_length'], $sig_limits[1]);
  2533. return 'signature_max_length';
  2534. }
  2535. // Too many lines?
  2536. if (!empty($sig_limits[2]) && substr_count($unparsed_signature, "\n") >= $sig_limits[2])
  2537. {
  2538. $txt['profile_error_signature_max_lines'] = sprintf($txt['profile_error_signature_max_lines'], $sig_limits[2]);
  2539. return 'signature_max_lines';
  2540. }
  2541. // Too many images?!
  2542. if (!empty($sig_limits[3]) && (substr_count(strtolower($unparsed_signature), '[img') + substr_count(strtolower($unparsed_signature), '<img')) > $sig_limits[3])
  2543. {
  2544. $txt['profile_error_signature_max_image_count'] = sprintf($txt['profile_error_signature_max_image_count'], $sig_limits[3]);
  2545. return 'signature_max_image_count';
  2546. }
  2547. // What about too many smileys!
  2548. $smiley_parsed = $unparsed_signature;
  2549. parsesmileys($smiley_parsed);
  2550. $smiley_count = substr_count(strtolower($smiley_parsed), '<img') - substr_count(strtolower($unparsed_signature), '<img');
  2551. if (!empty($sig_limits[4]) && $sig_limits[4] == -1 && $smiley_count > 0)
  2552. return 'signature_allow_smileys';
  2553. elseif (!empty($sig_limits[4]) && $sig_limits[4] > 0 && $smiley_count > $sig_limits[4])
  2554. {
  2555. $txt['profile_error_signature_max_smileys'] = sprintf($txt['profile_error_signature_max_smileys'], $sig_limits[4]);
  2556. return 'signature_max_smileys';
  2557. }
  2558. // Maybe we are abusing font sizes?
  2559. if (!empty($sig_limits[7]) && preg_match_all('~\[size=([\d\.]+)?(px|pt|em|x-large|larger)~i', $unparsed_signature, $matches) !== false && isset($matches[2]))
  2560. {
  2561. foreach ($matches[1] as $ind => $size)
  2562. {
  2563. $limit_broke = 0;
  2564. // Attempt to allow all sizes of abuse, so to speak.
  2565. if ($matches[2][$ind] == 'px' && $size > $sig_limits[7])
  2566. $limit_broke = $sig_limits[7] . 'px';
  2567. elseif ($matches[2][$ind] == 'pt' && $size > ($sig_limits[7] * 0.75))
  2568. $limit_broke = ((int) $sig_limits[7] * 0.75) . 'pt';
  2569. elseif ($matches[2][$ind] == 'em' && $size > ((float) $sig_limits[7] / 16))
  2570. $limit_broke = ((float) $sig_limits[7] / 16) . 'em';
  2571. elseif ($matches[2][$ind] != 'px' && $matches[2][$ind] != 'pt' && $matches[2][$ind] != 'em' && $sig_limits[7] < 18)
  2572. $limit_broke = 'large';
  2573. if ($limit_broke)
  2574. {
  2575. $txt['profile_error_signature_max_font_size'] = sprintf($txt['profile_error_signature_max_font_size'], $limit_broke);
  2576. return 'signature_max_font_size';
  2577. }
  2578. }
  2579. }
  2580. // The difficult one - image sizes! Don't error on this - just fix it.
  2581. if ((!empty($sig_limits[5]) || !empty($sig_limits[6])))
  2582. {
  2583. // Get all BBC tags...
  2584. preg_match_all('~\[img(\s+width=([\d]+))?(\s+height=([\d]+))?(\s+width=([\d]+))?\s*\](?:<br />)*([^<">]+?)(?:<br />)*\[/img\]~i', $unparsed_signature, $matches);
  2585. // ... and all HTML ones.
  2586. preg_match_all('~<img\s+src=(?:")?((?:http://|ftp://|https://|ftps://).+?)(?:")?(?:\s+alt=(?:")?(.*?)(?:")?)?(?:\s?/)?>~i', $unparsed_signature, $matches2, PREG_PATTERN_ORDER);
  2587. // And stick the HTML in the BBC.
  2588. if (!empty($matches2))
  2589. {
  2590. foreach ($matches2[0] as $ind => $dummy)
  2591. {
  2592. $matches[0][] = $matches2[0][$ind];
  2593. $matches[1][] = '';
  2594. $matches[2][] = '';
  2595. $matches[3][] = '';
  2596. $matches[4][] = '';
  2597. $matches[5][] = '';
  2598. $matches[6][] = '';
  2599. $matches[7][] = $matches2[1][$ind];
  2600. }
  2601. }
  2602. $replaces = array();
  2603. // Try to find all the images!
  2604. if (!empty($matches))
  2605. {
  2606. foreach ($matches[0] as $key => $image)
  2607. {
  2608. $width = -1; $height = -1;
  2609. // Does it have predefined restraints? Width first.
  2610. if ($matches[6][$key])
  2611. $matches[2][$key] = $matches[6][$key];
  2612. if ($matches[2][$key] && $sig_limits[5] && $matches[2][$key] > $sig_limits[5])
  2613. {
  2614. $width = $sig_limits[5];
  2615. $matches[4][$key] = $matches[4][$key] * ($width / $matches[2][$key]);
  2616. }
  2617. elseif ($matches[2][$key])
  2618. $width = $matches[2][$key];
  2619. // ... and height.
  2620. if ($matches[4][$key] && $sig_limits[6] && $matches[4][$key] > $sig_limits[6])
  2621. {
  2622. $height = $sig_limits[6];
  2623. if ($width != -1)
  2624. $width = $width * ($height / $matches[4][$key]);
  2625. }
  2626. elseif ($matches[4][$key])
  2627. $height = $matches[4][$key];
  2628. // If the dimensions are still not fixed - we need to check the actual image.
  2629. if (($width == -1 && $sig_limits[5]) || ($height == -1 && $sig_limits[6]))
  2630. {
  2631. $sizes = url_image_size($matches[7][$key]);
  2632. if (is_array($sizes))
  2633. {
  2634. // Too wide?
  2635. if ($sizes[0] > $sig_limits[5] && $sig_limits[5])
  2636. {
  2637. $width = $sig_limits[5];
  2638. $sizes[1] = $sizes[1] * ($width / $sizes[0]);
  2639. }
  2640. // Too high?
  2641. if ($sizes[1] > $sig_limits[6] && $sig_limits[6])
  2642. {
  2643. $height = $sig_limits[6];
  2644. if ($width == -1)
  2645. $width = $sizes[0];
  2646. $width = $width * ($height / $sizes[1]);
  2647. }
  2648. elseif ($width != -1)
  2649. $height = $sizes[1];
  2650. }
  2651. }
  2652. // Did we come up with some changes? If so remake the string.
  2653. if ($width != -1 || $height != -1)
  2654. $replaces[$image] = '[img' . ($width != -1 ? ' width=' . round($width) : '') . ($height != -1 ? ' height=' . round($height) : '') . ']' . $matches[7][$key] . '[/img]';
  2655. }
  2656. if (!empty($replaces))
  2657. $value = str_replace(array_keys($replaces), array_values($replaces), $value);
  2658. }
  2659. }
  2660. // Any disabled BBC?
  2661. $disabledSigBBC = implode('|', $disabledTags);
  2662. if (!empty($disabledSigBBC))
  2663. {
  2664. if (preg_match('~\[(' . $disabledSigBBC . '[ =\]/])~i', $unparsed_signature, $matches) !== false && isset($matches[1]))
  2665. {
  2666. $disabledTags = array_unique($disabledTags);
  2667. $txt['profile_error_signature_disabled_bbc'] = sprintf($txt['profile_error_signature_disabled_bbc'], implode(', ', $disabledTags));
  2668. return 'signature_disabled_bbc';
  2669. }
  2670. }
  2671. }
  2672. preparsecode($value);
  2673. return true;
  2674. }
  2675. /**
  2676. * Validate an email address.
  2677. *
  2678. * @param string $email
  2679. * @param int $memID = 0
  2680. * @return bool|string
  2681. */
  2682. function profileValidateEmail($email, $memID = 0)
  2683. {
  2684. global $smcFunc, $context;
  2685. $email = strtr($email, array('&#039;' => '\''));
  2686. // Check the name and email for validity.
  2687. if (trim($email) == '')
  2688. return 'no_email';
  2689. if (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $email) == 0)
  2690. return 'bad_email';
  2691. // Email addresses should be and stay unique.
  2692. $request = $smcFunc['db_query']('', '
  2693. SELECT id_member
  2694. FROM {db_prefix}members
  2695. WHERE ' . ($memID != 0 ? 'id_member != {int:selected_member} AND ' : '') . '
  2696. email_address = {string:email_address}
  2697. LIMIT 1',
  2698. array(
  2699. 'selected_member' => $memID,
  2700. 'email_address' => $email,
  2701. )
  2702. );
  2703. if ($smcFunc['db_num_rows']($request) > 0)
  2704. return 'email_taken';
  2705. $smcFunc['db_free_result']($request);
  2706. return true;
  2707. }
  2708. /**
  2709. * Reload a users settings.
  2710. */
  2711. function profileReloadUser()
  2712. {
  2713. global $sourcedir, $modSettings, $context, $cur_profile, $smcFunc, $profile_vars;
  2714. // Log them back in - using the verify password as they must have matched and this one doesn't get changed by anyone!
  2715. if (isset($_POST['passwrd2']) && $_POST['passwrd2'] != '')
  2716. {
  2717. require_once($sourcedir . '/Subs-Auth.php');
  2718. setLoginCookie(60 * $modSettings['cookieTime'], $context['id_member'], sha1(sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd2'])) . $cur_profile['password_salt']));
  2719. }
  2720. loadUserSettings();
  2721. writeLog();
  2722. }
  2723. /**
  2724. * Send the user a new activation email if they need to reactivate!
  2725. */
  2726. function profileSendActivation()
  2727. {
  2728. global $sourcedir, $profile_vars, $txt, $context, $scripturl, $smcFunc, $cookiename, $cur_profile, $language, $modSettings;
  2729. require_once($sourcedir . '/Subs-Post.php');
  2730. // Shouldn't happen but just in case.
  2731. if (empty($profile_vars['email_address']))
  2732. return;
  2733. $replacements = array(
  2734. 'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $context['id_member'] . ';code=' . $profile_vars['validation_code'],
  2735. 'ACTIVATIONCODE' => $profile_vars['validation_code'],
  2736. 'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $context['id_member'],
  2737. );
  2738. // Send off the email.
  2739. $emaildata = loadEmailTemplate('activate_reactivate', $replacements, empty($cur_profile['lngfile']) || empty($modSettings['userLanguage']) ? $language : $cur_profile['lngfile']);
  2740. sendmail($profile_vars['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  2741. // Log the user out.
  2742. $smcFunc['db_query']('', '
  2743. DELETE FROM {db_prefix}log_online
  2744. WHERE id_member = {int:selected_member}',
  2745. array(
  2746. 'selected_member' => $context['id_member'],
  2747. )
  2748. );
  2749. $_SESSION['log_time'] = 0;
  2750. $_SESSION['login_' . $cookiename] = serialize(array(0, '', 0));
  2751. if (isset($_COOKIE[$cookiename]))
  2752. $_COOKIE[$cookiename] = '';
  2753. loadUserSettings();
  2754. $context['user']['is_logged'] = false;
  2755. $context['user']['is_guest'] = true;
  2756. // Send them to the done-with-registration-login screen.
  2757. loadTemplate('Register');
  2758. $context['page_title'] = $txt['profile'];
  2759. $context['sub_template'] = 'after';
  2760. $context['title'] = $txt['activate_changed_email_title'];
  2761. $context['description'] = $txt['activate_changed_email_desc'];
  2762. // We're gone!
  2763. obExit();
  2764. }
  2765. /**
  2766. * Function to allow the user to choose group membership etc...
  2767. *
  2768. * @param int $memID id_member
  2769. */
  2770. function groupMembership($memID)
  2771. {
  2772. global $txt, $scripturl, $user_profile, $user_info, $context, $modSettings, $smcFunc;
  2773. $curMember = $user_profile[$memID];
  2774. $context['primary_group'] = $curMember['id_group'];
  2775. // Can they manage groups?
  2776. $context['can_manage_membergroups'] = allowedTo('manage_membergroups');
  2777. $context['can_manage_protected'] = allowedTo('admin_forum');
  2778. $context['can_edit_primary'] = $context['can_manage_protected'];
  2779. $context['update_message'] = isset($_GET['msg']) && isset($txt['group_membership_msg_' . $_GET['msg']]) ? $txt['group_membership_msg_' . $_GET['msg']] : '';
  2780. // Get all the groups this user is a member of.
  2781. $groups = explode(',', $curMember['additional_groups']);
  2782. $groups[] = $curMember['id_group'];
  2783. // Ensure the query doesn't croak!
  2784. if (empty($groups))
  2785. $groups = array(0);
  2786. // Just to be sure...
  2787. foreach ($groups as $k => $v)
  2788. $groups[$k] = (int) $v;
  2789. // Get all the membergroups they can join.
  2790. $request = $smcFunc['db_query']('', '
  2791. SELECT mg.id_group, mg.group_name, mg.description, mg.group_type, mg.online_color, mg.hidden,
  2792. IFNULL(lgr.id_member, 0) AS pending
  2793. FROM {db_prefix}membergroups AS mg
  2794. LEFT JOIN {db_prefix}log_group_requests AS lgr ON (lgr.id_member = {int:selected_member} AND lgr.id_group = mg.id_group)
  2795. WHERE (mg.id_group IN ({array_int:group_list})
  2796. OR mg.group_type > {int:nonjoin_group_id})
  2797. AND mg.min_posts = {int:min_posts}
  2798. AND mg.id_group != {int:moderator_group}
  2799. ORDER BY group_name',
  2800. array(
  2801. 'group_list' => $groups,
  2802. 'selected_member' => $memID,
  2803. 'nonjoin_group_id' => 1,
  2804. 'min_posts' => -1,
  2805. 'moderator_group' => 3,
  2806. )
  2807. );
  2808. // This beast will be our group holder.
  2809. $context['groups'] = array(
  2810. 'member' => array(),
  2811. 'available' => array()
  2812. );
  2813. while ($row = $smcFunc['db_fetch_assoc']($request))
  2814. {
  2815. // Can they edit their primary group?
  2816. if (($row['id_group'] == $context['primary_group'] && $row['group_type'] > 1) || ($row['hidden'] != 2 && $context['primary_group'] == 0 && in_array($row['id_group'], $groups)))
  2817. $context['can_edit_primary'] = true;
  2818. // If they can't manage (protected) groups, and it's not publically joinable or already assigned, they can't see it.
  2819. if (((!$context['can_manage_protected'] && $row['group_type'] == 1) || (!$context['can_manage_membergroups'] && $row['group_type'] == 0)) && $row['id_group'] != $context['primary_group'])
  2820. continue;
  2821. $context['groups'][in_array($row['id_group'], $groups) ? 'member' : 'available'][$row['id_group']] = array(
  2822. 'id' => $row['id_group'],
  2823. 'name' => $row['group_name'],
  2824. 'desc' => $row['description'],
  2825. 'color' => $row['online_color'],
  2826. 'type' => $row['group_type'],
  2827. 'pending' => $row['pending'],
  2828. 'is_primary' => $row['id_group'] == $context['primary_group'],
  2829. 'can_be_primary' => $row['hidden'] != 2,
  2830. // Anything more than this needs to be done through account settings for security.
  2831. 'can_leave' => $row['id_group'] != 1 && $row['group_type'] > 1 ? true : false,
  2832. );
  2833. }
  2834. $smcFunc['db_free_result']($request);
  2835. // Add registered members on the end.
  2836. $context['groups']['member'][0] = array(
  2837. 'id' => 0,
  2838. 'name' => $txt['regular_members'],
  2839. 'desc' => $txt['regular_members_desc'],
  2840. 'type' => 0,
  2841. 'is_primary' => $context['primary_group'] == 0 ? true : false,
  2842. 'can_be_primary' => true,
  2843. 'can_leave' => 0,
  2844. );
  2845. // No changing primary one unless you have enough groups!
  2846. if (count($context['groups']['member']) < 2)
  2847. $context['can_edit_primary'] = false;
  2848. // In the special case that someone is requesting membership of a group, setup some special context vars.
  2849. if (isset($_REQUEST['request']) && isset($context['groups']['available'][(int) $_REQUEST['request']]) && $context['groups']['available'][(int) $_REQUEST['request']]['type'] == 2)
  2850. $context['group_request'] = $context['groups']['available'][(int) $_REQUEST['request']];
  2851. }
  2852. /**
  2853. * This function actually makes all the group changes
  2854. *
  2855. * @param array $profile_vars
  2856. * @param array $post_errors
  2857. * @param int $memID id_member
  2858. * @return mixed
  2859. */
  2860. function groupMembership2($profile_vars, $post_errors, $memID)
  2861. {
  2862. global $user_info, $sourcedir, $context, $user_profile, $modSettings, $txt, $smcFunc, $scripturl, $language;
  2863. // Let's be extra cautious...
  2864. if (!$context['user']['is_owner'] || empty($modSettings['show_group_membership']))
  2865. isAllowedTo('manage_membergroups');
  2866. if (!isset($_REQUEST['gid']) && !isset($_POST['primary']))
  2867. fatal_lang_error('no_access', false);
  2868. checkSession(isset($_GET['gid']) ? 'get' : 'post');
  2869. $old_profile = &$user_profile[$memID];
  2870. $context['can_manage_membergroups'] = allowedTo('manage_membergroups');
  2871. $context['can_manage_protected'] = allowedTo('admin_forum');
  2872. // By default the new primary is the old one.
  2873. $newPrimary = $old_profile['id_group'];
  2874. $addGroups = array_flip(explode(',', $old_profile['additional_groups']));
  2875. $canChangePrimary = $old_profile['id_group'] == 0 ? 1 : 0;
  2876. $changeType = isset($_POST['primary']) ? 'primary' : (isset($_POST['req']) ? 'request' : 'free');
  2877. // One way or another, we have a target group in mind...
  2878. $group_id = isset($_REQUEST['gid']) ? (int) $_REQUEST['gid'] : (int) $_POST['primary'];
  2879. $foundTarget = $changeType == 'primary' && $group_id == 0 ? true : false;
  2880. // Sanity check!!
  2881. if ($group_id == 1)
  2882. isAllowedTo('admin_forum');
  2883. // Protected groups too!
  2884. else
  2885. {
  2886. $request = $smcFunc['db_query']('', '
  2887. SELECT group_type
  2888. FROM {db_prefix}membergroups
  2889. WHERE id_group = {int:current_group}
  2890. LIMIT {int:limit}',
  2891. array(
  2892. 'current_group' => $group_id,
  2893. 'limit' => 1,
  2894. )
  2895. );
  2896. list ($is_protected) = $smcFunc['db_fetch_row']($request);
  2897. $smcFunc['db_free_result']($request);
  2898. if ($is_protected == 1)
  2899. isAllowedTo('admin_forum');
  2900. }
  2901. // What ever we are doing, we need to determine if changing primary is possible!
  2902. $request = $smcFunc['db_query']('', '
  2903. SELECT id_group, group_type, hidden, group_name
  2904. FROM {db_prefix}membergroups
  2905. WHERE id_group IN ({int:group_list}, {int:current_group})',
  2906. array(
  2907. 'group_list' => $group_id,
  2908. 'current_group' => $old_profile['id_group'],
  2909. )
  2910. );
  2911. while ($row = $smcFunc['db_fetch_assoc']($request))
  2912. {
  2913. // Is this the new group?
  2914. if ($row['id_group'] == $group_id)
  2915. {
  2916. $foundTarget = true;
  2917. $group_name = $row['group_name'];
  2918. // Does the group type match what we're doing - are we trying to request a non-requestable group?
  2919. if ($changeType == 'request' && $row['group_type'] != 2)
  2920. fatal_lang_error('no_access', false);
  2921. // What about leaving a requestable group we are not a member of?
  2922. elseif ($changeType == 'free' && $row['group_type'] == 2 && $old_profile['id_group'] != $row['id_group'] && !isset($addGroups[$row['id_group']]))
  2923. fatal_lang_error('no_access', false);
  2924. elseif ($changeType == 'free' && $row['group_type'] != 3 && $row['group_type'] != 2)
  2925. fatal_lang_error('no_access', false);
  2926. // We can't change the primary group if this is hidden!
  2927. if ($row['hidden'] == 2)
  2928. $canChangePrimary = false;
  2929. }
  2930. // If this is their old primary, can we change it?
  2931. if ($row['id_group'] == $old_profile['id_group'] && ($row['group_type'] > 1 || $context['can_manage_membergroups']) && $canChangePrimary !== false)
  2932. $canChangePrimary = 1;
  2933. // If we are not doing a force primary move, don't do it automatically if current primary is not 0.
  2934. if ($changeType != 'primary' && $old_profile['id_group'] != 0)
  2935. $canChangePrimary = false;
  2936. // If this is the one we are acting on, can we even act?
  2937. if ((!$context['can_manage_protected'] && $row['group_type'] == 1) || (!$context['can_manage_membergroups'] && $row['group_type'] == 0))
  2938. $canChangePrimary = false;
  2939. }
  2940. $smcFunc['db_free_result']($request);
  2941. // Didn't find the target?
  2942. if (!$foundTarget)
  2943. fatal_lang_error('no_access', false);
  2944. // Final security check, don't allow users to promote themselves to admin.
  2945. if ($context['can_manage_membergroups'] && !allowedTo('admin_forum'))
  2946. {
  2947. $request = $smcFunc['db_query']('', '
  2948. SELECT COUNT(permission)
  2949. FROM {db_prefix}permissions
  2950. WHERE id_group = {int:selected_group}
  2951. AND permission = {string:admin_forum}
  2952. AND add_deny = {int:not_denied}',
  2953. array(
  2954. 'selected_group' => $group_id,
  2955. 'not_denied' => 1,
  2956. 'admin_forum' => 'admin_forum',
  2957. )
  2958. );
  2959. list ($disallow) = $smcFunc['db_fetch_row']($request);
  2960. $smcFunc['db_free_result']($request);
  2961. if ($disallow)
  2962. isAllowedTo('admin_forum');
  2963. }
  2964. // If we're requesting, add the note then return.
  2965. if ($changeType == 'request')
  2966. {
  2967. $request = $smcFunc['db_query']('', '
  2968. SELECT id_member
  2969. FROM {db_prefix}log_group_requests
  2970. WHERE id_member = {int:selected_member}
  2971. AND id_group = {int:selected_group}',
  2972. array(
  2973. 'selected_member' => $memID,
  2974. 'selected_group' => $group_id,
  2975. )
  2976. );
  2977. if ($smcFunc['db_num_rows']($request) != 0)
  2978. fatal_lang_error('profile_error_already_requested_group');
  2979. $smcFunc['db_free_result']($request);
  2980. // Log the request.
  2981. $smcFunc['db_insert']('',
  2982. '{db_prefix}log_group_requests',
  2983. array(
  2984. 'id_member' => 'int', 'id_group' => 'int', 'time_applied' => 'int', 'reason' => 'string-65534',
  2985. ),
  2986. array(
  2987. $memID, $group_id, time(), $_POST['reason'],
  2988. ),
  2989. array('id_request')
  2990. );
  2991. // Send an email to all group moderators etc.
  2992. require_once($sourcedir . '/Subs-Post.php');
  2993. // Do we have any group moderators?
  2994. $request = $smcFunc['db_query']('', '
  2995. SELECT id_member
  2996. FROM {db_prefix}group_moderators
  2997. WHERE id_group = {int:selected_group}',
  2998. array(
  2999. 'selected_group' => $group_id,
  3000. )
  3001. );
  3002. $moderators = array();
  3003. while ($row = $smcFunc['db_fetch_assoc']($request))
  3004. $moderators[] = $row['id_member'];
  3005. $smcFunc['db_free_result']($request);
  3006. // Otherwise this is the backup!
  3007. if (empty($moderators))
  3008. {
  3009. require_once($sourcedir . '/Subs-Members.php');
  3010. $moderators = membersAllowedTo('manage_membergroups');
  3011. }
  3012. if (!empty($moderators))
  3013. {
  3014. $request = $smcFunc['db_query']('', '
  3015. SELECT id_member, email_address, lngfile, member_name, mod_prefs
  3016. FROM {db_prefix}members
  3017. WHERE id_member IN ({array_int:moderator_list})
  3018. AND notify_types != {int:no_notifications}
  3019. ORDER BY lngfile',
  3020. array(
  3021. 'moderator_list' => $moderators,
  3022. 'no_notifications' => 4,
  3023. )
  3024. );
  3025. while ($row = $smcFunc['db_fetch_assoc']($request))
  3026. {
  3027. // Check whether they are interested.
  3028. if (!empty($row['mod_prefs']))
  3029. {
  3030. list(,, $pref_binary) = explode('|', $row['mod_prefs']);
  3031. if (!($pref_binary & 4))
  3032. continue;
  3033. }
  3034. $replacements = array(
  3035. 'RECPNAME' => $row['member_name'],
  3036. 'APPYNAME' => $old_profile['member_name'],
  3037. 'GROUPNAME' => $group_name,
  3038. 'REASON' => $_POST['reason'],
  3039. 'MODLINK' => $scripturl . '?action=moderate;area=groups;sa=requests',
  3040. );
  3041. $emaildata = loadEmailTemplate('request_membership', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
  3042. sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
  3043. }
  3044. $smcFunc['db_free_result']($request);
  3045. }
  3046. return $changeType;
  3047. }
  3048. // Otherwise we are leaving/joining a group.
  3049. elseif ($changeType == 'free')
  3050. {
  3051. // Are we leaving?
  3052. if ($old_profile['id_group'] == $group_id || isset($addGroups[$group_id]))
  3053. {
  3054. if ($old_profile['id_group'] == $group_id)
  3055. $newPrimary = 0;
  3056. else
  3057. unset($addGroups[$group_id]);
  3058. }
  3059. // ... if not, must be joining.
  3060. else
  3061. {
  3062. // Can we change the primary, and do we want to?
  3063. if ($canChangePrimary)
  3064. {
  3065. if ($old_profile['id_group'] != 0)
  3066. $addGroups[$old_profile['id_group']] = -1;
  3067. $newPrimary = $group_id;
  3068. }
  3069. // Otherwise it's an additional group...
  3070. else
  3071. $addGroups[$group_id] = -1;
  3072. }
  3073. }
  3074. // Finally, we must be setting the primary.
  3075. elseif ($canChangePrimary)
  3076. {
  3077. if ($old_profile['id_group'] != 0)
  3078. $addGroups[$old_profile['id_group']] = -1;
  3079. if (isset($addGroups[$group_id]))
  3080. unset($addGroups[$group_id]);
  3081. $newPrimary = $group_id;
  3082. }
  3083. // Finally, we can make the changes!
  3084. foreach ($addGroups as $id => $dummy)
  3085. if (empty($id))
  3086. unset($addGroups[$id]);
  3087. $addGroups = implode(',', array_flip($addGroups));
  3088. // Ensure that we don't cache permissions if the group is changing.
  3089. if ($context['user']['is_owner'])
  3090. $_SESSION['mc']['time'] = 0;
  3091. else
  3092. updateSettings(array('settings_updated' => time()));
  3093. updateMemberData($memID, array('id_group' => $newPrimary, 'additional_groups' => $addGroups));
  3094. return $changeType;
  3095. }
  3096. ?>